-2

I get XML:

$xml= simplexml_load_file('');

I have XML:

<ValCurs Date="22.12.2018" name="Foreign Currency Market">
 <Valute ID="R01011">
  <NumCode>036</NumCode>
  <CharCode>AUD</CharCode>
  <Nominal>1</Nominal>
  <Name>Dollar A</Name>
  <Value>10</Value>
 </Valute>
 <Valute ID="R01012">
  <NumCode>036</NumCode>
  <CharCode>AUD</CharCode>
  <Nominal>1</Nominal>
  <Name>Dollar B</Name>
  <Value>30</Value>
 </Valute>
 <Valute ID="R01013">
  <NumCode>036</NumCode>
  <CharCode>AUD</CharCode>
  <Nominal>1</Nominal>
  <Name>Dollar C</Name>
  <Value>20</Value>
 </Valute>
</ValCurs>

I need get Name and Value where Valute ID="R01013", how i can get it?
I try:

$xml->Valute_ID="R01013"->Name;
$xml->Valute_ID="R01013"->Value;

But it isn't work.

S. Artyom
  • 3
  • 3

1 Answers1

3

Using SimpleXML and XPath makes this quite trivial. First load the data (in this case my data is in a file called 'bok.xml' - but I assume you already have this part).

Then use XPath to look for a Valute element with the attribute ID (indicated by @ to show it's an attribute) and the value you want to find.

As XPath returns a list of matches, and there is only one then use [0], then you can use the ->Name etc to get the values your after...

$xml = simplexml_load_file("bok.xml");
$valute = $xml->xpath('//Valute[@ID="R01013"]');
echo $valute[0]->Name;
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55