Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 292
Anar Choi Avatar asked Jul 09 '13 00:07

Anar Choi


2 Answers

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.

like image 74
Prix Avatar answered Oct 13 '22 21:10

Prix


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.

like image 28
hakre Avatar answered Oct 13 '22 21:10

hakre