Skip to content

Pravin Paratey

Natural Language Processing, Data mining and Information Extraction consultant based in London.

Mar 29 2009
Mar 29 2009

XML DOM in Java

Of late, I have been working with Java. And one of the issues that I faced was XML parsing. With so many libraries available, I decided to stick to jaxp. What follows is sample code to Tree walk over the nodes:

TreeWalk.java
1 import java.io.File; 2 3 import javax.xml.parsers.DocumentBuilder; 4 import javax.xml.parsers.DocumentBuilderFactory; 5 6 import org.w3c.dom.Node; 7 import org.w3c.dom.NodeList; 8 9 10 public class Tester { 11 public static void main(String args[]) 12 { 13 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 14 factory.setNamespaceAware(false); 15 16 try { 17 DocumentBuilder builder = factory.newDocumentBuilder(); 18 org.w3c.dom.Document doc = builder.parse(new File(args[0])); 19 NodeList nodes1 = doc.getChildNodes(); 20 for(int i=0; i<nodes1.getLength(); i++) { 21 TreeWalk(nodes1.item(i), 0); 22 } 23 } 24 catch(Exception e) { 25 e.printStackTrace(); 26 } 27 } 28 29 private static void TreeWalk(Node n, int level) 30 { 31 if(n.getNodeType() != Node.TEXT_NODE) { 32 for(int i=0; i<level; i++) 33 System.out.print(" "); 34 System.out.print(n.getNodeName() + ":"); 35 } 36 else { 37 System.out.println(n.getNodeValue().trim()); 38 } 39 NodeList list = n.getChildNodes(); 40 for(int i=0; i<list.getLength(); i++) { 41 TreeWalk(list.item(i), level+1); 42 } 43 } 44 }

Latest Articles