Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php&;XML-按id获取项目_Php_Xml_Extract - Fatal编程技术网

Php&;XML-按id获取项目

Php&;XML-按id获取项目,php,xml,extract,Php,Xml,Extract,我有这样一个XML文件: <item id="55"> <title>Title</title> ... </item> 但是,如何基于id属性仅获取一项 我试过了,但没用 foreach ($rss->getElementsById('55') as $node) { 使用xpath仅选择具有特定属性的节点。在我的示例中,我使用了simplexml: $xml = simplexml_load_string($x); // assume

我有这样一个XML文件:

<item id="55">
<title>Title</title>
...
</item>
但是,如何基于id属性仅获取一项

我试过了,但没用

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

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

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

echo $node->title;
他是你的朋友。
$xml = simplexml_load_string($x); // assume XML in $x
$node = $xml->xpath("//item[@id='55']")[0];

echo $node->title;