Simple XMLElement Object
(
[IpStatus] => 1
[ti_pid_20642] => SimpleXmlElement Object
(
I have a SimpleXMLElment in above format and this XML is generated at run time and it's node values like ti_pid_20642
are partly dnymaic, for example ti_pid_3232
, ti-pid_2323
, ti_pid_anyumber
.
My question is how can I get these nodes values and it's children using PHP?
To get all node names that are used in an XML string with SimpleXML you can use the
SimpleXMLIterator
:Which could give you exemplary (you did not give any XML in your question, Demo):
If you have problems to provide a string that contains valid XML, take your existing SimpleXMLelement and create an XML string out of it:
However, if you like to get all tagnames from a SimpleXML object but you don't want to convert it to a string, you can create a recursive iterator for
SimpleXMLElement
as well:The usage of it would be similar (Demo):
It just depends on what you need.
This becomes less straight forward, with namespaced elements. Depending if you want to get the local names only or the namspace names or even the namespace URIs with the tagnames.
The given
SimpleXMLElementIterator
could be changed to support the iteration over elements across namespaces, by default simplexml only offers traversal over elements in the default namespace:You would then need to check for the namespace per each element- As an example a modified XML document making use of namespaces:
Combined with the update
SimpleXMLIterator
above the following example-code demonstrates the new behavior:Output (Demo):
Have fun.