Simplexml Get Element Content Based on Attribute Value

SimpleXML get element content based on attribute value

This XPath query on the SimpleXML object will return all DocSum nodes that have an Item child with value "Author" in the Name attribute and value "Olivera GC" in the text node:

$nodes = $xml->xpath('//DocSum[Item[@Name="Author" and .="Oliveira GC"]]');
$book = $nodes[0];
print_r($book);

simpleXML get node child based on attribute

Simply loop the poster elements and remember to cast the attribute values to strings, since you want to compare them (and probably output them) as strings:

$xml = simplexml_load_file('PosterData.xml');

foreach ($xml->poster as $poster) {
if ((string) $poster['id'] == 'minwage') {
echo (string) $poster->full_image['url'];
}
}

Get attribute value in SimpleXML object by element and another attribute value

You can use SimpleXML's xpath method to return an attribute of a node based on the value of another attribute:

$sxml = simplexml_load_string($xml);

$rate = (float) $sxml->xpath('./currencies/currency[@id="EUR"]/@rate')[0];

echo $rate;

Note that the method will always return an array, so we need to ask for the first element, and then cast the value to a float.

See https://eval.in/957883 for a full example

SimpleXML: Selecting Elements Which Have A Certain Attribute Value

Try this XPath:

/object/data[@type="me"]

Which reads as:

  • Select (/) children of the current element called object
  • Select (/) their children called data
  • Filter ([...]) that list to elements where ...
    • the attribute type (the @ means "attribute")
    • has the text value me

So:

$myDataObjects = $simplexml->xpath('/object/data[@type="me"]');

If object is not the root of your document, you might want to use //object/data[@type="me"] instead. The // means "find all descendents" rather than "find all children".

Use SimpleXML to select content of one node by its attribute

This outputs the expected Hello for me :

$string = <<<XML
<translation>
<home>
<button name="aaa">Hello</button>
<button name="bbb">World</button>
</home>
<office>
<button name="ccc">Foo</button>
<button name="ddd">Bar</button>
<string name="xxx">Sample</string>
</office>
</translation>
XML;
$xml = new SimpleXMLElement($string);
$data = $xml->xpath("//home/button[@name='aaa']")[0];
echo $data;

eval.in demo

SimpleXMLElement(): Get value of XML tag based off attribute value

The solution to the problem is as follows:

$summary = $xmlObj->xpath('doc/str[@name="summary"]');

if($summary) {
echo $summary[0];
}

It involves using XPath as a4arpan pointed out.

Get an attribute value using SimpleXML for PHP

You can use the attributes() function on the node to get it's attributes:

$xml_str = '<xml>
<node>
<someTag cp="c2">content</someTag>
</node>
</xml>';
$res = simplexml_load_string($xml_str);

$items = $res->xpath("//someTag");
var_dump((string) $items[0]->attributes()->cp);

The returned element is an SimpleXMLElement, so in order to use it I converted it to string (using the (string) cast).

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

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.

Simplexml get node by attribute

Try using DomDocument:

$xml = new DomDocument;
$xml->load('yourFile');
$xpath = new DomXpath($xml);

foreach ($xpath->query('//xml/opis[@lang="cz"]') as $rowNode) {
echo $rowNode->nodeValue; // will be 'this item'
}


Related Topics



Leave a reply



Submit