php simplexml根据字段的值获取特定项
有没有一种方法可以使用SimpleXML获取特定的项目 例如,我想使用以下示例xml获取ID设置为12437的项目的标题:php simplexml根据字段的值获取特定项,php,xml,rss,simplexml,Php,Xml,Rss,Simplexml,有没有一种方法可以使用SimpleXML获取特定的项目 例如,我想使用以下示例xml获取ID设置为12437的项目的标题: <items> <item> <title>blah blah 43534</title> <id>43534</id> </item> <item> <title>blah blah 12437</title>
<items>
<item>
<title>blah blah 43534</title>
<id>43534</id>
</item>
<item>
<title>blah blah 12437</title>
<id>12437</id>
</item>
<item>
<title>blah blah 7868</title>
<id>7868</id>
</item>
</items>
等等,等等,43534
43534
诸如此类的12437
12437
废话,废话7868
7868
这里有两种简单的方法来做你想做的事情,一种是对每个项目进行迭代,如下所示:
<?php
$str = <<<XML
<items>
<item>
<title>blah blah 43534</title>
<id>43534</id>
</item>
<item>
<title>blah blah 12437</title>
<id>12437</id>
</item>
<item>
<title>blah blah 7868</title>
<id>7868</id>
</item>
</items>
XML;
$data = new SimpleXMLElement($str);
foreach ($data->item as $item)
{
if ($item->id == 12437)
{
echo "ID: " . $item->id . "\n";
echo "Title: " . $item->title . "\n";
}
}
<?php
$str = <<<XML
<items>
<item>
<title>blah blah 43534</title>
<id>43534</id>
</item>
<item>
<title>blah blah 12437</title>
<id>12437</id>
</item>
<item>
<title>blah blah 7868</title>
<id>7868</id>
</item>
</items>
XML;
$data = new SimpleXMLElement($str);
// Here we find the element id = 12437 and get it's parent
$nodes = $data->xpath('//items/item/id[.="12437"]/parent::*');
$result = $nodes[0];
echo "ID: " . $result->id . "\n";
echo "Title: " . $result->title . "\n";
项目作为$item)
{
如果($item->id==12437)
{
回显“ID:”.$item->ID.\n;
回显“标题:“.$item->Title.\n”;
}
}
另一种方法是使用XPath,将您想要的确切数据固定到如下位置:
<?php
$str = <<<XML
<items>
<item>
<title>blah blah 43534</title>
<id>43534</id>
</item>
<item>
<title>blah blah 12437</title>
<id>12437</id>
</item>
<item>
<title>blah blah 7868</title>
<id>7868</id>
</item>
</items>
XML;
$data = new SimpleXMLElement($str);
foreach ($data->item as $item)
{
if ($item->id == 12437)
{
echo "ID: " . $item->id . "\n";
echo "Title: " . $item->title . "\n";
}
}
<?php
$str = <<<XML
<items>
<item>
<title>blah blah 43534</title>
<id>43534</id>
</item>
<item>
<title>blah blah 12437</title>
<id>12437</id>
</item>
<item>
<title>blah blah 7868</title>
<id>7868</id>
</item>
</items>
XML;
$data = new SimpleXMLElement($str);
// Here we find the element id = 12437 and get it's parent
$nodes = $data->xpath('//items/item/id[.="12437"]/parent::*');
$result = $nodes[0];
echo "ID: " . $result->id . "\n";
echo "Title: " . $result->title . "\n";
您希望为此使用Xpath。它基本上与中概述的完全相同,但在您的示例中,您不是决定属性值,而是决定元素值
但是,在Xpath中,您要查找的元素都是父元素。因此,制定Xpath表达式有点直截了当:
// Here we find the item element that has the child <id> element
// with node-value "12437".
list($result) = $data->xpath('(//items/item[id = "12437"])[1]');
$result->asXML('php://output');
它写为:选择所有
元素,这些元素是任何
元素的子元素,这些元素本身有一个名为
且值为“12437”
的子元素
现在有了这些缺失的东西:
(//items/item[id = "12437"])[1]
括号里写着:从所有这些
元素中,只选择第一个。根据您的结构,这可能是必要的,也可能不是
下面是完整的用法示例和:
只是xpath的一个小说明,它做的太多了。请使用中列出的属性://items/item[id=“12437”]
。对于更详细的解释。@hakre我已经知道了,但是我试图为初学者提供一个简单的答案,让他们理解,并向他展示我是如何通过上面的描述完成的。这个问题的参考问题在哪里?它肯定在什么地方,但我找不到。
<?php
/**
* php simplexml get a specific item based on the value of a field
* @lin https://stackoverflow.com/q/17537909/367456
*/
$str = <<<XML
<items>
<item>
<title>title of 43534</title>
<id>43534</id>
</item>
<item>
<title>title of 12437</title>
<id>12437</id>
</item>
<item>
<title>title of 7868</title>
<id>7868</id>
</item>
</items>
XML;
$data = new SimpleXMLElement($str);
// Here we find the item element that has the child <id> element
// with node-value "12437".
list($result) = $data->xpath('(//items/item[id = "12437"])[1]');
$result->asXML('php://output');