To get an attribute in the node, use node->attributes()->attributeName
SimpleXMLElement::attributes
(PHP 5 >= 5.0.1)
SimpleXMLElement::attributes — Identifies an element's attributes
Description
SimpleXMLElement attributes
([ string $ns
[, bool $is_prefix
]] )
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
Examples
Example #1 Interpret an XML string
<?php
$string = <<<XML
<a xmlns:b>
<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"
SimpleXMLElement::attributes
gillllberg at gmail dot com
05-Nov-2009 07:21
05-Nov-2009 07:21
skerr at mojavi dot org
10-Dec-2004 03:55
10-Dec-2004 03:55
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
27-May-2004 02:53
27-May-2004 02:53
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;
}
}
?>
