java中調用xml的方法:DocumentBuilderFactory

來源:互聯網
上載者:User

標籤:

具體的使用方法如下:

    (1)得到 DOM 解析器的工廠執行個體         

      DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();

    (2)從 DOM 工廠獲得 DOM 解析器

DocumentBuilder dombuilder=domfac.newDocumentBuilder(); 

    (3 )把要解析的 XML 文檔轉化為輸入資料流,以便 DOM 解析器解析它

InputStream is= new  FileInputStream("test1.xml");        

    (4)解析 XML 文檔的輸入資料流,得到一個 Document

Document doc=dombuilder.parse(is);

    (5)得到 XML 文檔的根節點

Element root=doc.getDocumentElement();

    (6)得到節點的子節點

NodeList books=root.getChildNodes();

Java代碼  
  1. package com.st.demo;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileInputStream;   
  5. import java.io.InputStream;   
  6.   
  7. import javax.xml.parsers.DocumentBuilder;   
  8. import javax.xml.parsers.DocumentBuilderFactory;   
  9.   
  10. import org.w3c.dom.Document;   
  11. import org.w3c.dom.Element;   
  12. import org.w3c.dom.Node;   
  13. import org.w3c.dom.NodeList;   
  14.   
  15. public class XmlReader {   
  16.     public static void main(String[] args) {   
  17.         XmlReader reader = new XmlReader();   
  18.     }   
  19.     public XmlReader(){   
  20.         DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();   
  21.         try {   
  22.             DocumentBuilder domBuilder = domfac.newDocumentBuilder();   
  23.             InputStream is = new FileInputStream(new File("D:/test1.xml"));   
  24.             Document doc = domBuilder.parse(is);   
  25.             Element root = doc.getDocumentElement();   
  26.             NodeList books = root.getChildNodes();   
  27.             if(books!=null){   
  28.                 for (int i = 0; i < books.getLength(); i++) {   
  29.                     Node book = books.item(i);   
  30.                      if(book.getNodeType()==Node.ELEMENT_NODE) {   
  31.                             //(7)取得節點的屬性值   
  32.                             String email=book.getAttributes().getNamedItem("email").getNodeValue();   
  33.                             System.out.println(email);   
  34.                             //注意,節點的屬性也是它的子節點。它的節點類型也是Node.ELEMENT_NODE  
  35.                             //(8)輪循子節點   
  36.                             for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()) {   
  37.                                 if(node.getNodeType()==Node.ELEMENT_NODE) {   
  38.                                     if(node.getNodeName().equals("name")) {   
  39.                                         String name=node.getNodeValue();   
  40.                                         String name1=node.getFirstChild().getNodeValue();   
  41.                                         System.out.println(name);   
  42.                                         System.out.println(name1);   
  43.                                     }   
  44.                                     if(node.getNodeName().equals("price")) {   
  45.                                         String price=node.getFirstChild().getNodeValue();   
  46.                                         System.out.println(price);   
  47.                                     }   
  48.                                 }   
  49.                             }   
  50.                      }   
  51.                 }   
  52.             }   
  53.         } catch (Exception e) {   
  54.             // TODO Auto-generated catch block   
  55.             e.printStackTrace();   
  56.         }   
  57.            
  58.     }   
  59. }  

 

Xml代碼  
  1. <?xml version="1.0" encoding="GB2312" standalone="no"?>  
  2. <books>  
  3.     <book email="zhoujunhui">  
  4.         <name>rjzjh</name>  
  5.         <price>jjjjjj</price>  
  6.     </book>  
  7. </books>  

java中調用xml的方法:DocumentBuilderFactory

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.