使用XML中的php特定参数查找

时间:2021-08-22 09:15:20

Hello i have the follow problem.

您好我有以下问题。

I have e XML file and i know the value of "uniquename=value2". I want extract the value of URL.

我有一个XML文件,我知道“uniquename = value2”的值。我想提取URL的值。

<set uniquename="value1">
  <items>
    <item>
      <units>
         ...
      </units>
    </item>
    <item>
      <units>
        ...
        <url>http://www.something</url>
        ...
      </units>
    </item>
  </items>
 </set>
 <set uniquename="value2">
  <items>
    <item>
      <units>
         ...
      </units>
    </item>
    <item>
      <units>
        ...
        <url>http://www.something2</url>
        ...
      </units>
    </item>
  </items>
 </set>

Someone has suggestions?

有人有建议吗?

Thanks to all.

谢谢大家。

1 个解决方案

#1


You can do it simply with simplexml and xpath:

你可以使用simplexml和xpath简单地完成它:

$x = simplexml_load_string($xml);
$nodes = $x->xpath('set[@uniquename="value2"]/items/item/units/url');

$url = (string)$nodes[0];

#1


You can do it simply with simplexml and xpath:

你可以使用simplexml和xpath简单地完成它:

$x = simplexml_load_string($xml);
$nodes = $x->xpath('set[@uniquename="value2"]/items/item/units/url');

$url = (string)$nodes[0];