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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
關(guān)于Java讀取xml文件的學(xué)習(xí)

一.java類(lèi)

  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)存中的一個(gè)鏡像,那么一旦獲取這個(gè)Document 就意味著可以通過(guò)對(duì)   
  9. // 內(nèi)存的操作來(lái)實(shí)現(xiàn)對(duì)XML的操作,首先***步獲取XML相關(guān)的Document   
  10. private Document doc = null;   
  11.  
  12. public void init(String xmlFile) throws Exception {   
  13. // 很明顯該類(lèi)是一個(gè)單例,先獲取產(chǎn)生DocumentBuilder工廠(chǎng)   
  14. // 的工廠(chǎng),在通過(guò)這個(gè)工廠(chǎng)產(chǎn)生一個(gè)DocumentBuilder,   
  15. // DocumentBuilder就是用來(lái)產(chǎn)生Document的   
  16. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();   
  17. DocumentBuilder db = dbf.newDocumentBuilder();   
  18. // 這個(gè)Document就是一個(gè)XML文件在內(nèi)存中的鏡像   
  19. doc = db.parse(new File(xmlFile));   
  20. }   
  21.  
  22. // 該方法負(fù)責(zé)把XML文件的內(nèi)容顯示出來(lái)   
  23. public void viewXML(String xmlFile) throws Exception {   
  24. this.init(xmlFile);   
  25. // 在xml文件里,只有一個(gè)根元素,先把根元素拿出來(lái)看看   
  26. Element element = doc.getDocumentElement();   
  27. System.out.println("根元素為:" + element.getTagName());   
  28.  
  29. NodeList nodeList = doc.getElementsByTagName("person");   
  30. System.out.println("book節(jié)點(diǎn)鏈的長(zhǎng)度:" + nodeList.getLength());   
  31.  
  32. Node fatherNode = nodeList.item(0);   
  33. System.out.println("父節(jié)點(diǎn)為:" + fatherNode.getNodeName());   
  34.  
  35. // 把父節(jié)點(diǎn)的屬性拿出來(lái)   
  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. + " 相對(duì)應(yīng)的屬性值為:" + 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. // 如果這個(gè)節(jié)點(diǎn)屬于Element ,再進(jìn)行取值   
  49. if (childNode instanceof Element) {   
  50. // System.out.println("子節(jié)點(diǎn)名為:"+childNode.getNodeName()+"相對(duì)應(yīng)的值為"+childNode.getFirstChild().getNodeValue());   
  51. System.out.println("子節(jié)點(diǎn)名為:" + childNode.getNodeName()   
  52. + "相對(duì)應(yīng)的值為" + 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. 中國(guó)郵電出版社 
  8.  
  9.  
  10. li 
  11. junjia 
  12. 24 
  13. 清華大學(xué)出版社 
  14.  
  15.  

三.輸出結(jié)果

根元素為:book
book節(jié)點(diǎn)鏈的長(zhǎng)度:2
父節(jié)點(diǎn)為:person
9
子節(jié)點(diǎn)名為:first相對(duì)應(yīng)的值為wang
子節(jié)點(diǎn)名為:last相對(duì)應(yīng)的值為laohu
子節(jié)點(diǎn)名為:age相對(duì)應(yīng)的值為25
子節(jié)點(diǎn)名為:version相對(duì)應(yīng)的值為中國(guó)郵電出版社


當(dāng)前文章:關(guān)于Java讀取xml文件的學(xué)習(xí)
分享路徑:http://m.5511xx.com/article/djcpeep.html