日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
關于Java讀取xml文件的學習

一.java類

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

二.xml文件

 
 
 
  1.  
  2.  
  3.  
  4. wang 
  5. laohu 
  6. 25 
  7. 中國郵電出版社 
  8.  
  9.  
  10. li 
  11. junjia 
  12. 24 
  13. 清華大學出版社 
  14.  
  15.  

三.輸出結(jié)果

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


網(wǎng)頁標題:關于Java讀取xml文件的學習
標題鏈接:http://www.5511xx.com/article/djcpeep.html