php simplexml get a specific item based on the value of a field

社会主义新天地 提交于 2019-11-26 14:54:05

问题


Is there a way i can get a specific item with SimpleXML ?

For example, i would like to get the title of an item having ID set to 12437 with this example 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>

回答1:


Here are 2 simple ways of doing what you want, one is iterating with each item like this:

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

The other would be using an XPath, to pin point the exact data you want like this:

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


You want to use Xpath for this. It's basically exactly the same as outlined in SimpleXML: Selecting Elements Which Have A Certain Attribute Value but in your case you're not deciding on an attribute value but on an element value.

However in Xpath for both the element you're looking for is the parent.So formulating the xpath expression is kind of straight forward:

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

Output (beautified):

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

So let's see the heart of this xpath query again:

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

It's written as: Select all <item> elements that are a children of any <items> elements which on their own have a child element named <id> with the value "12437".

And now with the missing stuff around:

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

The parenthesis around says: From all these <item> elements, pick the first one only. Depending on your structure this might or might not be necessary.

So here is the full usage example and 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');

So what you call a field in your questions title is by the book a child-element. Keep this in mind when searching for more complicated xpath queries that get you what you're looking for.



来源:https://stackoverflow.com/questions/17537909/php-simplexml-get-a-specific-item-based-on-the-value-of-a-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!