Java讀取xml檔案的學習

來源:互聯網
上載者:User

一.java類

  1. package com.java.test;
  2. import org.w3c.dom.*;
  3. import javax.xml.parsers.*;
  4. import java.io.*;
  5. public class JavaReadXml {
  6. // Document可以看作是XML在記憶體中的一個鏡像,那麼一旦擷取這個Document 就意味著可以通過對
  7. // 記憶體的操作來實現對XML的操作,首先第一步擷取XML相關的Document
  8. private Document doc = null;
  9. public void init(String xmlFile) throws Exception {
  10. // 很明顯該類是一個單例,先擷取產生DocumentBuilder工廠
  11. // 的工廠,在通過這個工廠產生一個DocumentBuilder,
  12. // DocumentBuilder就是用來產生Document的
  13. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  14. DocumentBuilder db = dbf.newDocumentBuilder();
  15. // 這個Document就是一個XML檔案在記憶體中的鏡像
  16. doc = db.parse(new File(xmlFile));
  17. }
  18. // 該方法負責把XML檔案的內容顯示出來
  19. public void viewXML(String xmlFile) throws Exception {
  20. this.init(xmlFile);
  21. // 在xml檔案裡,只有一個根項目,先把根項目拿出來看看
  22. Element element = doc.getDocumentElement();
  23. System.out.println("根項目為:" + element.getTagName());
  24. NodeList nodeList = doc.getElementsByTagName("person");
  25. System.out.println("book節點鏈的長度:" + nodeList.getLength());
  26. Node fatherNode = nodeList.item(0);
  27. System.out.println("父節點為:" + fatherNode.getNodeName());
  28. // 把父節點的屬性拿出來
  29. NamedNodeMap attributes = fatherNode.getAttributes();
  30. for (int i = 0; i < attributes.getLength(); i++) {
  31. Node attribute = attributes.item(i);
  32. System.out.println("book的屬性名稱為:" + attribute.getNodeName()
  33. + " 相對應的屬性值為:" + attribute.getNodeValue());
  34. }
  35. NodeList childNodes = fatherNode.getChildNodes();
  36. System.out.println(childNodes.getLength());
  37. for (int j = 0; j < childNodes.getLength(); j++) {
  38. Node childNode = childNodes.item(j);
  39. // 如果這個節點屬於Element ,再進行取值
  40. if (childNode instanceof Element) {
  41. // System.out.println("子節點名為:"+childNode.getNodeName()+"相對應的值為"+childNode.getFirstChild().getNodeValue());
  42. System.out.println("子節點名為:" + childNode.getNodeName()
  43. + "相對應的值為" + childNode.getFirstChild().getNodeValue());
  44. }
  45. }
  46. }
  47. public static void main(String[] args) throws Exception {
  48. JavaReadXml parse = new JavaReadXml();
  49. // 我的XML檔案
  50. parse.viewXML("person.xml");
  51. }
  52. }   

二.xml檔案

 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <book>
  3. <person>
  4. <first>wang</first>
  5. <last>laohu</last>
  6. <age>25</age>
  7. <version>中國郵電出版社</version>
  8. </person>
  9. <person>
  10. <first>li</first>
  11. <last>junjia</last>
  12. <age>24</age>
  13. <version>清華大學出版社</version>
  14. </person>
  15. </book>  

三.輸出結果

根項目為:book
book節點鏈的長度:2
父節點為:person
9
子節點名為:first相對應的值為wang
子節點名為:last相對應的值為laohu
子節點名為:age相對應的值為25
子節點名為:version相對應的值為中國郵電出版社

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.