关于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在内存中的一个镜像,那么一旦获取这个Document 就意味着可以通过对   
  9. // 内存的操作来实现对XML的操作,首先***步获取XML相关的Document   
  10. private Document doc = null;   
  11.  
  12. public void init(String xmlFile) throws Exception {   
  13. // 很明显该类是一个单例,先获取产生DocumentBuilder工厂   
  14. // 的工厂,在通过这个工厂产生一个DocumentBuilder,   
  15. // DocumentBuilder就是用来产生Document的   
  16. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();   
  17. DocumentBuilder db = dbf.newDocumentBuilder();   
  18. // 这个Document就是一个XML文件在内存中的镜像   
  19. doc = db.parse(new File(xmlFile));   
  20. }   
  21.  
  22. // 该方法负责把XML文件的内容显示出来   
  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节点链的长度:" + nodeList.getLength());   
  31.  
  32. Node fatherNode = nodeList.item(0);   
  33. System.out.println("父节点为:" + fatherNode.getNodeName());   
  34.  
  35. // 把父节点的属性拿出来   
  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. // 如果这个节点属于Element ,再进行取值   
  49. if (childNode instanceof Element) {   
  50. // System.out.println("子节点名为:"+childNode.getNodeName()+"相对应的值为"+childNode.getFirstChild().getNodeValue());   
  51. System.out.println("子节点名为:" + 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.  

三.输出结果

根元素为:book
book节点链的长度:2
父节点为:person
9
子节点名为:first相对应的值为wang
子节点名为:last相对应的值为laohu
子节点名为:age相对应的值为25
子节点名为:version相对应的值为中国邮电出版社

当前标题:关于Java读取xml文件的学习
标题网址:http://www.shufengxianlan.com/qtweb/news35/334435.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联