Php& XML - 按ID获取项目

时间:2013-08-07 00:26:07

标签: php xml extract

我有一个像这样的XML文件:

<item id="55">
<title>Title</title>
...
</item>

和一个将XML数据放在数组上的php文件......

foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        );
    array_push($feed, $item);
}

但是如何根据id属性只能只有

我尝试了这个但是没有用......

foreach ($rss->getElementsById('55') as $node) {

1 个答案:

答案 0 :(得分:1)

使用xpath仅选择具有特定属性的节点。在我的示例中,我使用simplexml

$xml = simplexml_load_string($x); // assume XML in $x
$node = $xml->xpath("//item[@id='55']")[0];

echo $node->title;
相关问题