How can I use XPath to select a node without retrieving all of its child nodes? For instance, in the following XML document:
<parentnode>
<node1 a="b" b="c">
<child1/>
<child2/>
... many many child nodes
<childN/>
</node1>
<node2/>
</parentnode>
I’d like to be able to select the ‘node1’ element in order to inspect its attributes, but without selecting the child nodes, which I don’t need to parse and could be thousands of elements, thus impacting the performance of the query (the output of which is used to build a sort of DOM tree with arrays and dictionaries in a 3rd party library).
Update: just to be clearer, the 3rd party library I mentioned is actually just an Objective-C wrapper around the libxml2 parser that builds a DOM tree made of Foundation classes with the result of any XPath query. The queries themselves are executed over an already parsed document (xmlDocPtr) that is reused for all queries, so yes, as many answers say, the document is already DOM’ed up at C level, but the Objective-C wrapper implementation produces the performance hit in this particular scenario. I could modify this library to optionally not fetch the selected node’s children, but I thought there would probably be a simple way to retrieve just the node’s attributes with the query.
If you just want the attributes, then just select the attributes: /parentnode/node1/@*
But (as noted in another answer) and Xpath processor still has to parse the whole file.
You won’t be saving much.
If you only want to parse part of the file and then stop after you’ve gotten the info you need, you should probably use SAX or some other API that gives you lower level control of the parsing.