public class XMLParser {
public static void main(String[] args){
try {
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = dBuilder.parse(new File("xml input"));
NodeList nl=doc.getDocumentElement().getChildNodes();
for(int k=0;k<nl.getLength();k++){
printTags((Node)nl.item(k));
}
} catch (Exception e) {/*err handling*/}
}
public static void printTags(Node nodes){
if(nodes.hasChildNodes() || nodes.getNodeType()!=3){
System.out.println(nodes.getNodeName()+" : "+nodes.getTextContent());
NodeList nl=nodes.getChildNodes();
for(int j=0;j<nl.getLength();j++)printTags(nl.item(j));
}
}
}
Recursively loop through and print out all the xml child tags in the document, in case you don't have to change the code to handle dynamic changes in xml, provided it's a well formed xml.