I';我对在PHP中解析XML感到困惑

I';我对在PHP中解析XML感到困惑,php,xml,parsing,Php,Xml,Parsing,如何使用PHP解析XML数据?我只想获取名为 <profile xmlns="mysite.com"> <confirmationNumber>128686132</confirmationNumber> <merchantRefNum>123456</merchantRefNum> <customerEmail>test@test.com</customerEmail> </profile>

如何使用PHP解析XML数据?我只想获取名为

<profile xmlns="mysite.com">
 <confirmationNumber>128686132</confirmationNumber>
 <merchantRefNum>123456</merchantRefNum>
 <customerEmail>test@test.com</customerEmail>
</profile>

但是我不知道如何选择特定的元素。

您可以尝试使用该函数,并使用XML作为对象。或者使用cast转换为数组。

您可以尝试该函数,并使用XML作为对象。或者使用cast转换为数组。

您应该查看。

您应该查看。

您不需要SimpleXML的遍历方法。如果您已经在一个中使用了它,只需使用:

$xml = simplexml_load_string($decodedMessage);
然后,访问内容非常简单:


您不需要SimpleXML的遍历方法。如果您已经在一个中使用了它,只需使用:

$xml = simplexml_load_string($decodedMessage);
然后,访问内容非常简单:


处理它的最简单方法之一是在google上查找一个名为xml2array的函数,因为在PHP中处理关联数组要比在XML中容易得多。

处理它的最简单方法之一是在google上查找一个名为xml2array的函数,因为关联数组在PHP中比XML更容易处理。

您可以试试这个

$xml = simplexml_load_file($filePath) or die ("Unable to load XML file!"); 
//Access XML Data
echo "confirmation Number: " . $xml->confirmationNumber;
echo "merchant Ref Num: " . $xml->confirmationNumber; 
echo "customer Email: " . $xml->customerEmail;
你可以试试这个

$xml = simplexml_load_file($filePath) or die ("Unable to load XML file!"); 
//Access XML Data
echo "confirmation Number: " . $xml->confirmationNumber;
echo "merchant Ref Num: " . $xml->confirmationNumber; 
echo "customer Email: " . $xml->customerEmail;

如果希望在较大的文档中找到该元素,可以使用

例如,对于XML

<some>
  <hierarchy>
    <profile>
      <etc>et cetera</etc>
    </profile>
  </hierarchy>
</some>

等等

您只需执行
$sxe->xpath('//profile')
,它将返回文档中所有概要文件元素的数组(作为sxe,然后您可以对其进行处理)。如果只有一个profile元素,那么就完成了,但是如果有很多元素,则需要更具体的XPath。

如果要在更大的文档中找到该元素,可以使用

例如,对于XML

<some>
  <hierarchy>
    <profile>
      <etc>et cetera</etc>
    </profile>
  </hierarchy>
</some>

等等
您只需执行
$sxe->xpath('//profile')
,它将返回文档中所有概要文件元素的数组(作为sxe,然后您可以对其进行处理)。如果只有一个profile元素,那么就完成了,但是如果有很多元素,则需要更具体的XPath