PHP:获取父节点,其中child = 'value'


PHP: get parent node where child = 'value'

我有这个xml文件:

<friends>
    <friend>
        <name>xxx</name>
        <pays>France</pays>
    </friend>
    <friend>
        <name>yyy</name>
        <country>France</country>
    </friend>
    <friend>
        <name>zzz</name>
        <country>USA</country>
    </friend>
</friends>

获取我的数据,我使用这个php代码:

$xml = simplexml_load_file('friends.xml');
$friendsXML = $xml->friend;

可以正常工作,但是返回所有的好友。

现在我只想检索来自法国的朋友:

country = 'france'.
谁能帮我做那件事?

我会使用XPath来完成这样的事情。试一试:

 $res =  $xml->xpath('friend[country = "france"]');
 echo $res[0];