How to Access Element Attributes with Simplexml

Accessing @attribute from SimpleXML

You can get the attributes of an XML element by calling the attributes() function on an XML node. You can then var_dump the return value of the function.

More info at php.net
http://php.net/simplexmlelement.attributes

Example code from that page:

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}

How to get attributes of XML root element using SimpleXMLElement

When loadinG to simplexml, attributes of the root element became attributes of the simpleXml object. So, you can get it just

$str = '<?xml version="1.0" encoding="iso-8859-1"?>
<wwwjob id="32cca11IACH" method="Delete">
</wwwjob>';
$vacancyXML = simplexml_load_string($str);

echo $vacancyXML['method']; // Delete

demo

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

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

Get attributes and values using SimpleXML

This code should work:

$xml = new SimpleXMLElement($xmlString);
$source = $xml->event->attributes()->source;
$timestamp = $xml->event->attributes()->timestamp;
$subject = $xml->event->subject;
$action = $xml->event->action;
$message = $xml->event->message;

... where $xmlString is the string of the xml file.

Read up on how to use simpleXML here.

Hope this helped and good luck!

Looping through SimpleXMLElement to access attributes

SimpleXML is an array-like object. Cheat sheet:

  • Unprefixed child elements as numeric-index or traversable
    • Does not include prefixed elements (NOTE, I really mean prefixed, not null-namespace! SimpleXMLElement handling of namespaces is a strange and arguably broken.)
    • first child: $sxe[0]
    • new SimpleXMLElement with a subset of matching elements: $sxe->ROWS, $sxe->{'ROWS'}
    • iterate children: foreach ($sxe as $e), $sxe->children()
    • Text content: (string) $sxe. SimpleXMLElement always returns another SimpleXMLElement, so if you need a string cast it explicitly!
  • Prefixed child elements:
    • $sxe->children('http://example.org') returns a new SimpleXMLElement with elements
      in the matching namespace, with namespace stripped so you can use it like the previous section.
  • Attributes in null namespace as key-index:
    • specific attribute: `$sxe['attribute-name']
    • all attributes: $sxe->attributes()
    • $sxe->attributes() returns a special SimpleXMLElement that shows attributes as both child elements and attributes, so both the following work:
    • $sxe->attributes()->COMP_ID
    • $a = $sxe->attributes(); $a['COMP_ID'];
    • Value of an attribute: coerce to string (string) $sxe['attr-name']
  • Attributes in other namespaces:
    • all attributes: $sxe->attributes('http://example.org')
    • specific attribute: $sxe_attrs = $sxe->attributes('http://example.org'); $sxe_attrs['attr-name-without-prefix']

What you want is:

$xml = '<ROOT><ROWS COMP_ID="165462"/><ROWS COMP_ID="165463"/></ROOT>';

$sxe = simplexml_load_string($xml);

foreach($sxe->ROWS as $row) {
$id = (string) $row['COMP_ID'];
}

Access @attributes data in SimpleXMLElement in PHP

Did you try:

echo (string)$xml->product->attributes()->id;

That should give you access to the attributes.

If you have more than 1 product, it may be

echo (string)$xml->product[0]->attributes()->id;

You can also access the attributes using regular array notation such as:

$xml->product['id']; // instead of $xml->product->attributes()->id

See Example #5 from the SimpleXML Examples as well as the manual page on SimpleXMLElement::attributes() for more information.

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


Related Topics



Leave a reply



Submit