Skip to main content

Effective XML processing with DOM and XPath in Java

Analysis of many projects yields advice and suggested code

    Element root    = doc.getDocumentElement();
    Node configNode = root.getFirstChild();
    NodeList childNodes = configNode.getChildNodes();
    for (int childNum = 0; 
	 childNum < childNodes.getLength(); 
	 childNum++) 
    {
      if ( childNodes.item(childNum).getNodeType() == Node.ELEMENT_NODE ) {
	Element child = (Element) childNodes.item( childNum ) ;
	if ( child.getTagName().equals( "header" ) ) {
	  // Do something with the header
	  System.out.print("Got a header!\n");
	}
      }
    }

Return to article