【问题标题】:PHP: get parent node where child = 'value' [duplicate]PHP:获取child ='value'的父节点[重复]
【发布时间】:2015-07-10 17:17:45
【问题描述】:

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

谁能帮我做这件事?

【问题讨论】:

标签: php xml parsing


【解决方案1】:

我会为这样的事情使用 XPath。试试:

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

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多