Using PHP, how do I get an entire subset of nodes from an XML document? I can retrieve something like:
<?xml version="1.0" encoding="utf-8"?>
<people>
<certain>
<name>Jane Doe</name>
<age>21</age>
</certain>
<certain>
<certain>
<name>John Smith</name>
<age>34</age>
</certain>
</people>
But what if I only want to return the child nodes of like this?
<certain>
<name>Jane Doe</name>
<age>21</age>
</certain>
<certain>
<certain>
<name>John Smith</name>
<age>34</age>
</certain>
EDIT: I'm trying to get a subset of XML and pass that directly, not an object like simplexml would give me. I am basically trying to get PHP to do what .NET's OuterXml does... return literally the above subset of XML as is... no interpreting or converting or creating a new XML file or anything... just extract those nodes in situ and pass them on. Am I going to have to get the XML file, parse out what I need and then rebuild it as a new XML file? If so then I need to get rid of the <?xml version="1.0" encoding="utf-8"?>
bit... ugh.
See http://www.php.net/manual/en/domdocument.getelementsbytagname.php
The answer would be to use XPath.
Take 2
So apparently you want to copy some nodes from a document into another document? SimpleXML doesn't support that. DOM has methods for that but they're kind of annoying to use. Which one are you using? Here's what I use: SimpleDOM. In fact, it's really SimpleXML augmented with DOM's methods.
That routine finds all
<certain/>
node via XPath, then appends them to the new document.The answer turned out to be a combination of the xpath suggestion and outputting with
asXML()
.Using the example given by Josh Davis:
You could use
DOMDocument.GetElementsByTagName
or you could:Use XPath?
Use DOM and XPath. Xpath allows you to select nodes (and values) from an XML DOM.
Demo: https://eval.in/162149
DOMDocument::saveXml() has a context argument. If provided it saves that node as XML. Much like
outerXml()
. PHP is able to register your own classes for the DOM nodes, too. So it is even possible to add anouterXML()
function to element nodes.Demo: https://eval.in/162157