I need to query an XML string in PHP were I know the names of the nodes I am looking for but I might not necessarily know the path to the node.
I think I can do this with xpath but I can’t work out how to set up a relative path to look anywhere in the document, could someone point me in the right direction.
I am currently trying to acheive this with simpleXML, but if there is a better way of doing this I would love to hear it.
Don’t use regexes to parse XML!
The descendant (double-slash) operator in xpath will search all descendants for a match.
The above is equivalent to the DOM method
getElementsByTagNameIf you have a general idea of where the element lives, you can use the descendant operator to search below a certain node:
If you need to use
DOMDocumentinstead ofSimpleXML, you can issue xpath instructions with theDOMXPathobject if necessary. (Left as an exercise for the reader.)