搜尋此網誌

2013年4月21日 星期日

How to read xml files in Java(Java讀取xml文件)

SAX is the Simple API for XML, originally a Java-only API employee.xml :
 <?xml version="1.0" encoding="UTF-8"?>  
 <company>  
      <employee>  
           <firstname>Michael</firstname>  
           <lastname>Jordan</lastname>  
      </employee>  
      <employee>  
           <firstname>Larry</firstname>  
           <lastname>Bird</lastname>  
      </employee>  
      <employee>  
           <firstname>Tiger</firstname>  
           <lastname>Woods</lastname>  
      </employee>  
 </company>  

W3c DOM sample :(from Java Tips):
 package dom;  
 import java.io.File;  
 import javax.xml.parsers.DocumentBuilder;  
 import javax.xml.parsers.DocumentBuilderFactory;  
 import org.w3c.dom.Document;  
 import org.w3c.dom.Element;  
 import org.w3c.dom.Node;  
 import org.w3c.dom.NodeList;  
 public class W3cDomDemo {  
      public static void main(String argv[]) {  
           try {  
                String path = "./bin/dom/employee.xml";  
                File file = new File(path);  
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
                DocumentBuilder db = dbf.newDocumentBuilder();  
                Document doc = db.parse(file);  
                doc.getDocumentElement().normalize();  
                System.out.println("Root element "  
                          + doc.getDocumentElement().getNodeName());  
                NodeList nodeLst = doc.getElementsByTagName("employee");  
                System.out.println("Information of all employees");  
                for (int s = 0; s < nodeLst.getLength(); s++) {  
                     Node fstNode = nodeLst.item(s);  
                     if (fstNode.getNodeType() == Node.ELEMENT_NODE) {  
                          Element fstElmnt = (Element) fstNode;  
                          NodeList fstNmElmntLst = fstElmnt  
                                    .getElementsByTagName("firstname");  
                          Element fstNmElmnt = (Element) fstNmElmntLst.item(0);  
                          NodeList fstNm = fstNmElmnt.getChildNodes();  
                          System.out.println("First Name : "  
                                    + ((Node) fstNm.item(0)).getNodeValue());  
                          NodeList lstNmElmntLst = fstElmnt  
                                    .getElementsByTagName("lastname");  
                          Element lstNmElmnt = (Element) lstNmElmntLst.item(0);  
                          NodeList lstNm = lstNmElmnt.getChildNodes();  
                          System.out.println("Last Name : "  
                                    + ((Node) lstNm.item(0)).getNodeValue());  
                     }  
                }  
           } catch (Exception e) {  
                e.printStackTrace();  
           }  
      }  
 }  

using Dom4j: dom4j , you must have dom4j library to execute below sample code
 package dom;  
 import org.dom4j.Document;  
 import org.dom4j.dom.DOMDocumentFactory;  
 import org.dom4j.io.SAXReader;  
 import org.w3c.dom.Element;  
 import org.w3c.dom.Node;  
 import org.w3c.dom.NodeList;  
 public class NativeDomDemo {  
   public static void main(String[] args) {  
        String path = "./bin/dom/employee.xml";  
     new NativeDomDemo().run(path);  
   }  
   public NativeDomDemo() {  
   }  
      private void run(String xmlFile) {  
           try {  
                parseDOM(xmlFile);  
           } catch (Exception e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  
   protected void parseDOM(String xmlFile) throws Exception {  
     System.out.println("Loading document: " + xmlFile);  
     SAXReader reader = new SAXReader(DOMDocumentFactory.getInstance());  
     Document document = reader.read(xmlFile);  
     System.out.println("Created <dom4j> document: " + document);  
     if (document instanceof org.w3c.dom.Document) {  
       org.w3c.dom.Document domDocument = (org.w3c.dom.Document) document;  
       System.out.println("Created W3C DOM document: " + domDocument);  
       processDOM(domDocument);  
     } else {  
          System.out.println("FAILED to make a native W3C DOM document!!");  
     }  
   }  
   protected void processDOM(org.w3c.dom.Document doc) throws Exception {  
        NodeList nodeLst = doc.getElementsByTagName("employee");  
           System.out.println("Information of all employees");  
           for (int s = 0; s < nodeLst.getLength(); s++) {  
                Node fstNode = nodeLst.item(s);  
                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {  
                     Element fstElmnt = (Element) fstNode;  
                     NodeList fstNmElmntLst = fstElmnt  
                               .getElementsByTagName("firstname");  
                     Element fstNmElmnt = (Element) fstNmElmntLst.item(0);  
                     NodeList fstNm = fstNmElmnt.getChildNodes();  
                     System.out.println("First Name : "  
                               + ((Node) fstNm.item(0)).getNodeValue());  
                     NodeList lstNmElmntLst = fstElmnt  
                               .getElementsByTagName("lastname");  
                     Element lstNmElmnt = (Element) lstNmElmntLst.item(0);  
                     NodeList lstNm = lstNmElmnt.getChildNodes();  
                     System.out.println("Last Name : "  
                               + ((Node) lstNm.item(0)).getNodeValue());  
                }  
           }  
   }  
 }