XML解析使用php simplexml_load_file


XML Parsing using php simplexml_load_file

我的xml文件example.xml如下:

<?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>
我的php代码如下:
$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

问题:
当我使用PHP simplexml_load_file方法获得值$xml->ID根据上面的代码,它给出了345678。但是当我试着求$xml->Groups->Group->名称解析器混淆和给出Group->Name而不是给Aaron。解析器会混淆我想是组和组。获取名称Aaron的正确代码是什么

我们已经尝试重现您的问题,但仍然让Aaron在第三行。如果您仍然面临同样的问题,请检查下面的代码,它将解决您的问题。

$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