import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import java.io.File;

public class  HelloDOMXML {

private Document _doc;

public HelloDOMXML(Document doc) {
 _doc = doc;
}//HelloDOMXML

public void recorrer() {
 NodeList nodos = _doc.getElementsByTagName("Item");
 int longitud = nodos.getLength();
 Text dato=null ;
 for (int i=0;i<longitud;i++) {
   Element nodo = (Element) nodos.item(i);
   Element elementID= (Element)nodo.getElementsByTagName("ID").item(0);
   dato=(Text)elementID.getFirstChild() ;
   System.out.println(elementID.getNodeName()+" "+dato.getData());
   Element elementTitle= (Element)nodo.getElementsByTagName("Title").item(0);
   dato=(Text)elementTitle.getFirstChild() ;
   System.out.println(elementTitle.getNodeName()+" "+dato.getData());
   Element elementQuantity= (Element)nodo.getElementsByTagName("Quantity").item(0);
   dato=(Text)elementQuantity.getFirstChild() ;
   System.out.println(elementQuantity.getNodeName()+" "+dato.getData());
   Element elementUnitPrice= (Element)nodo.getElementsByTagName("UnitPrice").item(0);
   dato=(Text)elementUnitPrice.getFirstChild() ;
   System.out.println(elementUnitPrice.getNodeName()+" "+dato.getData());
 }//for
 Element cliente = (Element) _doc.getElementsByTagName("Customer").item(0);
 if (cliente.hasAttribute("rfc")) {
   System.out.println( cliente.getAttribute("rfc"));
 }
}//recorrer

public static void main(String [] args) {
 try {
  DocumentBuilderFactory dbf = 
         DocumentBuilderFactory.newInstance(); 

  DocumentBuilder db =  dbf.newDocumentBuilder(); 
  Document doc=db.parse(new File(args[0]));
  HelloDOMXML hello=new HelloDOMXML(doc);
  hello.recorrer();
 } catch (Exception ex) {
   ex.printStackTrace();
 }
}//main
}//HelloDOMXML
