Iterate all XML node generation in java DOM

I want to check if an XML document contains a human element anywhere. I can check all the elements of the first generation very simply:

NodeList nodeList = root.getChildNodes(); for(int i=0; i<nodeList.getLength(); i++){ Node childNode = nodeList.item(i); if (childNode.getNodeName() == "person") { //do something with it } } 

And I can add more loops to go into sub-elements, but I would need to know how many nested loops I need to insert in order to determine how far to drill in the document. I could have nested 10 cycles, and as a result, the human element nested 12 elements in depth in this document. I need to remove the element, no matter how deep it is nested.

Is there a way to assemble elements from a whole document? How to return text values ​​of all tags as an array or iterate over it?

Something similar to the python elementtree 'findall' method is possible:

 for person in tree.findall('//person'): personlist.append(person) 
+7
java dom xml
source share
5 answers

As mmyers states you can use recursion for this problem.

 doSomethingWithAll(root.getChildNodes()); void doSomethingWithAll(NodeList nodeList) { for (int i = 0; i < nodeList.getLength(); i++) { Node childNode = nodeList.item(i); if (childNode.getNodeName().equals("person")) { //do something with it } NodeList children = childNode.getChildNodes(); if (children != null) { doSomethingWithAll(children); } } } 
+10
source share

I see three possibilities (two of which the others answered):

  • Use recursion.
  • Use XPath (maybe a little too much for this problem, but if you have a lot of queries like this, there’s definitely something to learn). Use the help of kdgregory; A quick look at the api showed that it is a little painful to use directly.
  • If what you have is actually Document (that is, if root is Document ), you can use Document.getElementsByTagName
+10
source share

What is XPath for? To get all the elements with the name "man", here is the expression:

 //person 

It can be painful to use the XPath JDK API directly. I prefer the wrappers I wrote in the XML Practical Library: http://practicalxml.sourceforge.net/

And here is the tutorial that I wrote (on the XPath JDK in general, but mentions XPathWrapper): http://www.kdgregory.com/index.php?page=xml.xpath

+4
source share

Here is the formatted version:

 Element root = xmlData.getDocumentElement(); NodeList children = root.getChildNodes(); public void doSomethingWithAllToConsole(NodeList nodeList, String tabs) { for(int i=0; i<nodeList.getLength(); i++){ //print current node & values Node childNode = nodeList.item(i); if(childNode.getNodeType()==Node.ELEMENT_NODE){ System.out.print(tabs + childNode.getNodeName()); if(childNode.getFirstChild()!=null && childNode.getFirstChild().getNodeType()==Node.TEXT_NODE && !StringUtil.isNullOrEmpty(childNode.getFirstChild().getNodeValue()) ){ System.out.print(" = " + childNode.getFirstChild().getNodeValue()); } System.out.println(); } //recursively iterate through child nodes NodeList children = childNode.getChildNodes(); if (children != null) { doSomethingWithAllToConsole(children, tabs+"\t"); } } } 
+2
source share

Besides Document.getElementsByTagName() or XPath , you can also use jOOX , a library that I created for easier XML access and manipulation. jOOX wraps the standard Java API and adds jquery -like methods. Your Python code snippet will then translate into this Java code:

 // Just looking for tag names for (Element person : $(tree).find("person")) { personlist.append(person); } // Use XPath for more elaborate queries for (Element person : $(tree).xpath("//person")) { personlist.append(person); } 
0
source share

All Articles