I need to print arbitrary SimpleXML objects in a specific manner, with special handling of attribute nodes.
The problem is that SimpleXML elements and attributes seem to use exactly the same class, attribute node even pretends to support attributes()
method, and SimpleXML hides its internals, so there doesn't seem to be any way to tell type of node (short of generating XML and reparsing it).
Both give identical result:
$element = new SimpleXMLElement('<foo>test</foo>');
echo $element;
print_r($element);
$element = new SimpleXMLElement('<foo attr="test" />');
echo $element['attr'];
print_r($element['attr']);
Is there a hidden property/method that allows identifying type of node in SimpleXML? Equivalent of DOM's $node->nodeType
or $node instanceof DOMAttr
? (I can't use DOM instead, support for SimpleXML is core requirement).