import java.io.*;
import org.w3c.dom.*;
import org.w3c.*;
import javax.xml.parsers.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class use_dom {
public static void main(String[] args){
try{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.
newDocumentBuilder
DocumentBuilder();
Document d= parser.parse("test.xml");
Element root = d.getDocumentElement();
if (root.getNodeName().compareTo("purchaseOrder")==0){
System.out.println(" orderDate==> "
+ root.getAttribute("orderDate"));
Node n = root.getFirstChild();
if (n != null){
do {
if (n.getNodeType() == Node.ELEMENT_NODE
&& n.getNodeName().compareTo("item")==0){
Node n2 = n.getFirstChild();
if (n2!=null){
do{
if (n2.getNodeType()
== Node.ELEMENT_NODE){
System.out.println(
n2.getNodeName()
+ "==>" +
n2.getFirstChild().getNodeValue()
);
}
}while((n2=n2.getNextSibling())!=null);
}
}
}while ((n=n.getNextSibling()) != null );
} } }
catch (Exception e){
System.out.println("exception occurred ==>"+e);
} } }