org.w3c.dom處理xml的常用方法


Java代碼  
  1. import java.io.File;  
  2.   
  3. import javax.xml.parsers.DocumentBuilder;  
  4. import javax.xml.parsers.DocumentBuilderFactory;  
  5.   
  6. import org.w3c.dom.Document;  
  7. import org.w3c.dom.Element;  
  8. import org.w3c.dom.NodeList;  
  9.   
  10. public class DomTest1  
  11. {  
  12.     public static void main(String[] args) throws Exception  
  13.     {  
  14.         // step 1: 獲得dom解析器工廠(工作的作用是用於創建具體的解析器)  
  15.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
  16.           
  17. //      System.out.println("class name: " + dbf.getClass().getName());  
  18.           
  19.         // step 2:獲得具體的dom解析器  
  20.         DocumentBuilder db = dbf.newDocumentBuilder();  
  21.           
  22. //      System.out.println("class name: " + db.getClass().getName());  
  23.           
  24.         // step3: 解析一個xml文檔,獲得Document對象(根結點)  
  25.         Document document = db.parse(new File("candidate.xml"));  
  26.           
  27.         NodeList list = document.getElementsByTagName("PERSON");  
  28.           
  29.         for(int i = 0; i < list.getLength(); i++)  
  30.         {  
  31.             Element element = (Element)list.item(i);  
  32.               
  33.             String content = element.getElementsByTagName("NAME").item(0).getFirstChild().getNodeValue();  
  34.               
  35.             System.out.println("name:" + content);  
  36.               
  37.             content = element.getElementsByTagName("ADDRESS").item(0).getFirstChild().getNodeValue();  
  38.               
  39.             System.out.println("address:" + content);  
  40.               
  41.             content = element.getElementsByTagName("TEL").item(0).getFirstChild().getNodeValue();  
  42.               
  43.             System.out.println("tel:" + content);  
  44.               
  45.             content = element.getElementsByTagName("FAX").item(0).getFirstChild().getNodeValue();  
  46.               
  47.             System.out.println("fax:" + content);  
  48.               
  49.             content = element.getElementsByTagName("EMAIL").item(0).getFirstChild().getNodeValue();  
  50.               
  51.             System.out.println("email:" + content);  
  52.               
  53.             System.out.println("--------------------------------------");  
  54.         }  
  55.     }  
  56. }  

Java代碼  
  1. import java.io.File;  
  2.   
  3. import javax.xml.parsers.DocumentBuilder;  
  4. import javax.xml.parsers.DocumentBuilderFactory;  
  5.   
  6. import org.w3c.dom.Attr;  
  7. import org.w3c.dom.Comment;  
  8. import org.w3c.dom.Document;  
  9. import org.w3c.dom.Element;  
  10. import org.w3c.dom.NamedNodeMap;  
  11. import org.w3c.dom.Node;  
  12. import org.w3c.dom.NodeList;  
  13.   
  14. /** 
  15.  * 使用遞歸解析給定的任意一個xml文檔並且將其內容輸出到命令行上 
  16.  * @author zhanglong 
  17.  * 
  18.  */  
  19. public class DomTest3  
  20. {  
  21.     public static void main(String[] args) throws Exception  
  22.     {  
  23.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
  24.         DocumentBuilder db = dbf.newDocumentBuilder();  
  25.           
  26.         Document doc = db.parse(new File("student.xml"));  
  27.         //獲得根元素結點  
  28.         Element root = doc.getDocumentElement();  
  29.           
  30.         parseElement(root);  
  31.     }  
  32.       
  33.     private static void parseElement(Element element)  
  34.     {  
  35.         String tagName = element.getNodeName();  
  36.           
  37.         NodeList children = element.getChildNodes();  
  38.           
  39.         System.out.print("<" + tagName);  
  40.           
  41.         //element元素的所有屬性所構成的NamedNodeMap對象,需要對其進行判斷  
  42.         NamedNodeMap map = element.getAttributes();  
  43.           
  44.         //如果該元素存在屬性  
  45.         if(null != map)  
  46.         {  
  47.             for(int i = 0; i < map.getLength(); i++)  
  48.             {  
  49.                 //獲得該元素的每一個屬性  
  50.                 Attr attr = (Attr)map.item(i);  
  51.                   
  52.                 String attrName = attr.getName();  
  53.                 String attrValue = attr.getValue();  
  54.                   
  55.                 System.out.print(" " + attrName + "=\"" + attrValue + "\"");  
  56.             }  
  57.         }  
  58.           
  59.         System.out.print(">");  
  60.           
  61.         for(int i = 0; i < children.getLength(); i++)  
  62.         {  
  63.             Node node = children.item(i);  
  64.             //獲得結點的類型  
  65.             short nodeType = node.getNodeType();  
  66.               
  67.             if(nodeType == Node.ELEMENT_NODE)  
  68.             {  
  69.                 //是元素,繼續遞歸  
  70.                 parseElement((Element)node);  
  71.             }  
  72.             else if(nodeType == Node.TEXT_NODE)  
  73.             {  
  74.                 //遞歸出口  
  75.                 System.out.print(node.getNodeValue());  
  76.             }  
  77.             else if(nodeType == Node.COMMENT_NODE)  
  78.             {  
  79.                 System.out.print("<!--");  
  80.                   
  81.                 Comment comment = (Comment)node;  
  82.                   
  83.                 //註釋內容  
  84.                 String data = comment.getData();  
  85.                   
  86.                 System.out.print(data);  
  87.                   
  88.                 System.out.print("-->");  
  89.             }  
  90.         }  
  91.           
  92.         System.out.print("</" + tagName + ">");  
  93.     }  
  94. }  
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章