Using
->children();
at the end, that is with the default namespace, will give you a child-collection of zero elements. As it's empty, SimpleXML internally runs some optimizations. Accessing it again might lead to errors:
$nodes->attributes()['value']
Warning: main(): Node no longer exists in ...
Perhaps the best way is to just leave it out and name the children you're looking for:
$nodes = ... ->extension->children($namespaces["keyvalue"])->kv;
print_r($nodes[1]->attributes()['value']);
Gives this output:
SimpleXMLElement Object
(
[0] => ZBh5ralfPl
)
As it's the second <kv>
element (zero-based) of which the attribute "value" is accessed.
If you want to leave the concrete element out of it, just leave it out (do not add ->children()
). The same example:
$nodes = $xml->response->extension->children($namespaces["keyvalue"])->
extension->children($namespaces["keyvalue"]);
print_r($nodes[1]->attributes()['value']);
This gives exactly the same output:
SimpleXMLElement Object
(
[0] => ZBh5ralfPl
)
as there are only <kv>
elements as children there, so the numbering does not change.
Hope this helps to shed some light. Perhaps better than all this is to use xpath:
$xml->registerXPathNamespace('kv', $namespaces["keyvalue"]);
var_dump($xml->xpath('//kv:kv[@key = "AUTH"]/@value')[0]);
Which gives:
class SimpleXMLElement#6 (1) {
public $@attributes =>
array(1) {
'value' =>
string(10) "ZBh5ralfPl"
}
}
Xpath is specialized on traversal. This is even better when it comes to namespaces.