JAVA读取xml文件

来源:互联网 发布:安徽幻维网络是培训吗 编辑:程序博客网 时间:2024/05/17 08:59
  1. 一.java类

  2. package com.java.test;   
  3.  
  4. import org.w3c.dom.*;   
  5. import javax.xml.parsers.*;   
  6. import java.io.*;   
  7.  
  8. public class JavaReadXml   
  9. // Document可以看作是XML在内存中的一个镜像,那么一旦获取这个Document 就意味着可以通过对   
  10. // 内存的操作来实现对XML的操作,首先第一步获取XML相关的Document   
  11. private Document doc null  
  12.  
  13. public void init(String xmlFile) throws Exception   
  14. // 很明显该类是一个单例,先获取产生DocumentBuilder工厂   
  15. // 的工厂,在通过这个工厂产生一个DocumentBuilder,   
  16. // DocumentBuilder就是用来产生Document的   
  17. DocumentBuilderFactory dbf DocumentBuilderFactory.newInstance();   
  18. DocumentBuilder db dbf.newDocumentBuilder();   
  19. // 这个Document就是一个XML文件在内存中的镜像   
  20. doc db.parse(new File(xmlFile));   
  21.   
  22.  
  23. // 该方法负责把XML文件的内容显示出来   
  24. public void viewXML(String xmlFile) throws Exception   
  25. this.init(xmlFile);   
  26. // 在xml文件里,只有一个根元素,先把根元素拿出来看看   
  27. Element element doc.getDocumentElement();   
  28. System.out.println("根元素为:" element.getTagName());   
  29.  
  30. NodeList nodeList doc.getElementsByTagName_r("person");   
  31. System.out.println("book节点链的长度:" nodeList.getLength());   
  32.  
  33. Node fatherNode nodeList.item(0);   
  34. System.out.println("父节点为:" fatherNode.getNodeName());   
  35.  
  36. // 把父节点的属性拿出来   
  37. NamedNodeMap attributes fatherNode.getAttributes();   
  38.  
  39. for (int 0attributes.getLength(); i++)   
  40. Node attribute attributes.item(i);   
  41. System.out.println("person的属性名为:" attribute.getNodeName() 
  42. 相对应的属性值为:" attribute.getNodeValue());//原作者这块写的是book的属性名,经调试应该是person的   
  43.   
  44.  
  45. NodeList childNodes fatherNode.getChildNodes();   
  46. System.out.println(childNodes.getLength());   
  47. for (int 0childNodes.getLength(); j++)   
  48. Node childNode childNodes.item(j);   
  49. // 如果这个节点属于Element ,再进行取值   
  50. if (childNode instanceof Element)   
  51. // System.out.println("子节点名为:"+childNode.getNodeName()+"相对应的值为"+childNode.getFirstChild().getNodeValue());   
  52. System.out.println("子节点名为:" childNode.getNodeName()   
  53. "相对应的值为" childNode.getFirstChild().getNodeValue());   
  54.   
  55.   
  56.  
  57.   
  58.  
  59. public static void main(String[] args) throws Exception   
  60. JavaReadXml parse new JavaReadXml();   
  61.  
  62. // 我的XML文件   
  63. parse.viewXML("person.xml");   
  64.   
  65.  
  66. 二.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> 
    16. 三.输出结果

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

       
     

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 wifi登录名忘记了怎么办 苹果手机微信图纸打不开怎么办 手机qq邮箱文件打不开怎么办 12360忘记用户名和密码怎么办 刚开店铺没生意怎么办 淘宝账户不符合注销条件怎么办 网易邮箱登录密码忘记了怎么办 q号密码忘记了怎么办 志愿者注册忘记密码和用户名怎么办 w10电脑语言栏不见了怎么办 w10美式键盘没了怎么办 xp电脑开机密码忘记了怎么办 电脑开机密码到期忘记改怎么办 电脑账户数据库密码忘了怎么办 微信不能拍摄了怎么办 华硕笔记本用户名密码忘了怎么办 学信网登录密码用户名搞忘怎么办 电脑密码输入错误会被锁怎么办 电脑被锁机软件设了密码怎么办 电脑密码被锁了怎么办 电脑xp密码忘了怎么办 三星账户账号密码忘了怎么办 三星账户密码忘记了怎么办 w7账号被禁用了怎么办 笔记本用户名密码忘记了怎么办 笔记本忘记用户名和密码怎么办 苹果手机忘记用户名和密码怎么办 w10电脑忘了密码怎么办 联想台式电脑密码忘记了怎么办 新电脑用一会就很烫怎么办 电脑忘了密码怎么办啊 戴尔笔记本账户密码忘记了怎么办 华硕台式电脑开不了机怎么办 手机号申请qq账号上限了怎么办 店铺不干了被告商标侵权怎么办 咸鱼违规被投诉侵犯商标权怎么办 房子买了70年后怎么办 买房付完首付后怎么办 手机b站忘记密码怎么办 护士电子注册没有激活码怎么办 大王卡变3g网是怎么办