简体   繁体   中英

XML Parsing using php simplexml_load_file

<?xml version="1.0" encoding="UTF-8"?>
<Declaration>
        <ID>345678</ID>
      <TransID>1473909194263</TransID>
    <Time>2016-02-24T22:13:15</Time>
<Groups>
   <Group>
     <Name>Aaron</Name>
       <SelectedFields/>
   </Group>
</Groups>
<Scores/>
   <RelatedVariables>
      <RelatedVariable dataType="Number">
         <Value>5555</Value>
       </RelatedVariable>
      <RelatedVariable dataType="Number">
         <Value>9999</Value>
      </RelatedVariable>
   </RelatedVariables>
</Declaration>

$xml=simplexml_load_file("example.xml") or die("Error: Cannot create object");

echo $xml->ID;                      //Gives 345678

echo $xml->Groups->Group->Name;     // Gives Group->Name rather than Aaron

Question :
When I used PHP simplexml_load_file method to get the value $xml->ID by the above code it gave 345678. But when I tried to get the value of $xml->Groups->Group->Name the parser confused and gave Group->Name rather than giving Aaron. The parser confuse with Groups and Group I think. Whats the correct code to get the Name Aaron

We have tried to reproduce your issue but still getting Aaron on the third line. If you are still facing the same issue please check below code, it will solve your problem.

$xml=simplexml_load_file("example.xml") or die("Error: Cannot create object");
$xml = json_decode(json_encode($xml),true);
echo $xml['ID']; //Gives 345678
echo $xml['Groups']['Group']['Name']; //Gives Aaron

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM