user3599604 user3599604 - 3 months ago 13
PHP Question

How can I get a particular node from SimpleXML

My XML is the following: http://api.napiarfolyam.hu/?valuta=eur

I would like to get only the 'vetel' value where the 'bank' is mnb.

My PHP is the following:

<?php
$xml=simplexml_load_file("http://api.napiarfolyam.hu/?valuta=eur") or die("Error: Cannot create object");

$vetel = (string) $xml->valuta->bank->vetel;
?>

Answer

This one should be the solution but the problem is, according to your xml file, bank mnb has no vetel. So the iterator can't see bank mnb.

$xml=simplexml_load_file("http://api.napiarfolyam.hu/?valuta=eur") or die("Error: Cannot create object");

foreach($xml->valuta->item as $item){
    if($item->bank == "mnb"){
      echo $item->vetel;
      break; 
    }
}