Note that you must provide the namespace if you want to access an attribute of a non-default namespace:
Consider the following example:
<?php
$xml = <<<XML
<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Table Foo="Bar" ss:ExpandedColumnCount="7">
</Table>
</Workbook>
XML;
$sxml = new SimpleXMLElement($xml);
/**
* Access attribute of default namespace
*/
var_dump((string) $sxml->Table[0]['Foo']);
// outputs: 'Bar'
/**
* Access attribute of non-default namespace
*/
var_dump((int) $sxml->Table[0]['ExpandedColumnCount']);
// outputs: 0
var_dump((int) $sxml->Table[0]->attributes('ss', TRUE)->ExpandedColumnCount);
// outputs: '7'
?>
SimpleXMLElement::attributes
(PHP 5 >= 5.0.1)
SimpleXMLElement::attributes — Identifies an element's attributes
Description
public SimpleXMLElement SimpleXMLElement::attributes
([ string
$ns = NULL
[, bool $is_prefix = false
]] )This function provides the attributes and values defined within an xml tag.
Note: SimpleXML has made a rule of adding iterative properties to most methods. They cannot be viewed using var_dump() or anything else which can examine objects.
Parameters
-
ns -
An optional namespace for the retrieved attributes
-
is_prefix -
Default to
FALSE
Return Values
Returns a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag.
Returns NULL if called on a SimpleXMLElement
object that already represents an attribute and not a tag.
Examples
Example #1 Interpret an XML string
<?php
$string = <<<XML
<a>
<foo name="one" game="lonely">1</foo>
</a>
XML;
$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
?>
The above example will output:
name="one" game="lonely"
chris at chlab dot ch ¶
1 year ago
leap ¶
2 years ago
If you want to save the value of an attribute into an array, typecast it to a string.
<?php
// stores an xml-element-object
$dataStore['value'] = $attributes->myValue
// stores the value of the attribute
$dataStore['value'] = (string)$attributes->myValue
?>
gillllberg at gmail dot com ¶
3 years ago
To get an attribute in the node, use node->attributes()->attributeName
Xeoncross ¶
3 years ago
It is really simple to access attributes using array form. However, you must convert them to strings or ints if you plan on passing the values to functions.
<?php
SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 55555
)
[text] => "hello world"
)
?>
Then using a function
<?php
function xml_attribute($object, $attribute)
{
if(isset($object[$attribute]))
return (string) $object[$attribute];
}
?>
I can get the "id" like this
<?php
print xml_attribute($xml, 'id'); //prints "55555"
?>
sarlak ¶
2 years ago
<?php
$att = 'attribueName';
// You can access an element's attribute just like this :
$attribute = $element->attributes()->$att;
// This will save the value of the attribute, and not the objet
$attribute = (string)$element->attributes()->$att;
// You also can edit it this way :
$element->attributes()->$att = 'New value of the attribute';
?>
totalwipeout at gmail dot com ¶
7 months ago
Tip to get a real array of all attributes of a node (not SimpleXML's object acting like an array)
<?php
//- $node is a SimpleXMLElement object
$atts_object = $node->attributes(); //- get all attributes, this is not a real array
$atts_array = (array) $atts_object; //- typecast to an array
//- grab the value of '@attributes' key, which contains the array your after
$atts_array = $atts_array['@attributes'];
var_dump($atts_object); //- outputs object(SimpleXMLElement)[19]
//- public '@attributes' => ...
var_dump($atts_array); //- outputs array (size=11) ...
?>
Hope this helps!
alan at performantsystems dot com ¶
3 years ago
So lets say you have database type data in an XML string called $xmlstring with the key or item ID as an XML Attribute and all content data as regular XML Elements, as above. SimpleXML processes the Attributes as an array, so we can play along and push the Attributes into an array. Then we can get the value of any specific Attribute we want by addressing it by name, such as "ID".
Considering this data:
<?xml version="1.0" encoding="utf-8"?>
<data>
<item ID="30001">
<Company>Navarro Corp.</Company>
</item>
<item ID="30002">
<Company>Performant Systems</Company>
</item>
<item ID="30003">
<Company>Digital Showcase</Company>
</item>
</data>
Example of listing both the ID Attribute and Company Element values:
<?php
$xmlObject = new SimpleXMLElement($xmlstring);
foreach ($xmlObject->children() as $node) {
$arr = $node->attributes(); // returns an array
print ("ID=".$arr["ID"]); // get the value of this attribute
print (" Company=".$node->Company);
print ("<p><hr>");
}
?>
adrian at foeder dot de ¶
4 days ago
in order to get a possibly present xml:space attribute, use the following construct:
<?php
$simpleXmlElement->element->attributes('http://www.w3.org/XML/1998/namespace')->space;
?>
This will, for example, return 'preserve' if set.
Just passing by 'xml' as namespace argument of the attribute() method didn't work out for me. Passing by the complete namespace URI works also if it is not explicitly defined in the underlying XML document.
Also, $simpleXmlElement->getNamespaces() does not return anything of use.
Andrei ¶
2 years ago
Reading the attributes of the root element with name space prefixes as in an Atom feed
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gd="http://schemas.google.com/g/2005" gd:etag="W/"Ck4EQXYzeCp7ImA9WhZWFE4."">
<?php
$xml = @simplexml_load_file($feed);
$att_gd = $xml->attributes("gd",1);
$Etag = $att_gd["etag"];
?>
skerr at mojavi dot org ¶
8 years ago
You can also access the node as an array to get attributes:
<?php
$xml = simplexml_load_file('file.xml');
echo 'Attribute: ' . $xml['attribute'];
?>
inge at elektronaut dot no ¶
9 years ago
here's a simple function to get an attribute by name, based on the example
<?php
function findAttribute($object, $attribute) {
foreach($object->attributes() as $a => $b) {
if ($a == $attribute) {
$return = $b;
}
}
if($return) {
return $return;
}
}
?>
