PHP:获取父节点where child ='value'

时间:2015-07-10 17:17:45

标签: php xml parsing

我有这个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'.

任何人都可以帮我这么做吗?

1 个答案:

答案 0 :(得分:2)

我使用XPath来做这样的事情。尝试:

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