【问题标题】:php simplexml get a specific item based on the value of a fieldphp simplexml 根据字段的值获取特定项目
【发布时间】:2013-07-06 10:49:04
【问题描述】:

有没有一种方法可以让我使用 SimpleXML 获得特定项目?

例如,我想使用此示例 xml 获取 ID 设置为 12437 的项目的标题:

<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>

【问题讨论】:

  • 这个问题的参考问题到底在哪里?它一定存在于某个地方,但我找不到它。

标签: php xml rss simplexml


【解决方案1】:

这里有两种简单的方法来做你想做的事,一种是像这样迭代每个项目:

<?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";
    }
}

Live DEMO.

另一种是使用 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);
// 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";

Live DEMO.

【讨论】:

【解决方案2】:

您想为此使用 Xpath。它与SimpleXML: Selecting Elements Which Have A Certain Attribute Value 中概述的基本完全相同,但在您的情况下,您不是在决定属性值,而是在元素值上。

但是,在 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');

输出(美化):

<item>
  <title>title of 12437</title>
  <id>12437</id>
</item>

让我们再次看看这个 xpath 查询的核心:

//items/item[id = "12437"]

写成:选择所有&lt;item&gt;元素,这些元素是任何&lt;items&gt;元素的子元素,这些元素本身有一个名为&lt;id&gt;的子元素,其值为"12437"

现在还有缺少的东西:

(//items/item[id = "12437"])[1]

周围的括号说:从所有这些&lt;item&gt; 元素中,只选择第一个。根据您的结构,这可能是必要的,也可能不是。

这里是完整的用法示例和online demo

<?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');

因此,您在问题标题中所说的字段在本书中是子元素。在搜索更复杂的 xpath 查询时请记住这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    相关资源
    最近更新 更多