-1

My xml file example.xml is as follows :

<?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>

My php code as follows:

$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

Parfait
  • 104,375
  • 17
  • 94
  • 125
Explorer
  • 295
  • 1
  • 12
  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – ThW Sep 18 '16 at 08:50
  • Hi Thw My question is different from the above suggested question by you – Explorer Sep 18 '16 at 10:36
  • Can't reproduce your issue. I certainly get *Aaron* on third line. – Parfait Sep 18 '16 at 15:14

1 Answers1

0

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
Keval Rathi
  • 978
  • 6
  • 21