Skip to content
Advertisement

Accessing @attribute from SimpleXML

I am having a problem accessing the @attribute section of my SimpleXML object. When I var_dump the entire object, I get the correct output, and when I var_dump the rest of the object (the nested tags), I get the correct output, but when I follow the docs and var_dump $xml->OFFICE->{'@attributes'}, I get an empty object, despite the fact that the first var_dump clearly shows that there are attributes to output.

Anyone know what I am doing wrong here/how I can make this work?

Advertisement

Answer

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";
}

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement