How can I get a subset of the Java XML org.w3c.dom.Document?

I have an XML object org.w3c.dom.Document.

It looks something like this:

<A>
  <B>
     <C/>
     <D/>
     <E/>
     <F/>
   </B>
   <G>
     <H/>
     <H/>
     <J/>
   </G>
 </A>

      

How can I convert the Document object so that it removes the root node and returns a different subtype of the Document object (selected by name) that looks like this:

<G>
   <H/>
   <H/>
   <J/>
</G>

      

I hope for something like this:

...
Document doc = db.parse(file);
Document subdoc = doc.getDocumentSubsetByName("G"); //imaginary method name
NodeList nodeList = subdoc.getElementsByTagName("H");

      

But I find it hard to find such a thing.


The answer turns out to be something like this:

...
Document doc = db.parse();
doc.getDocumentElement.normalize();
NodeList a = doc.getElementsByTagName("A");
Element AsubNode = null;
if (a.item(0) != null) {
   AsubNode = (Element) a.item(0);
   nodeList = AsubNode.getElementsByTagName("G");
...

      

+2


source to share


2 answers


You can just use getElementsByTagName("G")

to get the elements G

, then select one of them and call getElementsByTagName("H")

.



0


source


Of course, you can always use XPath to accomplish the same:

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.NodeList;

final XPath xpath = XPathFactory.newInstance().newXPath();
final NodeList list = (NodeList) xpath.evaluate("/A/G/H", 
    doc.getDocumentElement(), XPathConstants.NODESET);

      



It starts to pay off as the path to your elements gets more complex (requires attribute predicates, etc.).

0


source







All Articles