Document多种方式解析xml文件

前端之家收集整理的这篇文章主要介绍了Document多种方式解析xml文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

例:如下xml(你可以自己添加删除一些节点进行测试)

  1. <?xml version="1.0" encoding="gbk"?>
  2. <root>
  3. <pnode>
  4. <province>
  5. <pid>10056</pid>
  6. <pname>山东</pname>
  7. <cnode>
  8. <city>
  9. <cid>11017</cid>
  10. <cname>潍坊</cname>
  11. <znode>
  12. <zone>
  13. <zid>11018</zid>
  14. <zname>农垦</zname>
  15. <nnode>
  16. <node>
  17. <nid>11019</nid>
  18. <nname>试验点</nname>
  19. </node>
  20. <node>
  21. <nid>10020</nid>
  22. <nname>测试</nname>
  23. </node>
  24. </nnode>
  25. <zid>11018</zid>
  26. <zname>农垦a </zname>
  27. <nnode>
  28. <node>
  29. <nid>1101a 9</nid>
  30. <nname>试验点a </nname>
  31. </node>
  32. <node>
  33. <nid>1102a 0</nid>
  34. <nname>测试a </nname>
  35. </node>
  36. </nnode>
  37. </zone>
  38. </znode>
  39. </city>
  40. <city>
  41. <cid>11028</cid>
  42. <cname>诸城</cname>
  43. <znode>
  44. <zone>
  45. <zid>11029</zid>
  46. <zname>诸城都是烟田</zname>
  47. <nnode>
  48. <node>
  49. <nid>11030</nid>
  50. <nname>不信你去看</nname>
  51. </node>
  52. <node>
  53. <nid>11041</nid>
  54. <nname>你去看啊</nname>
  55. </node>
  56. </nnode>
  57. </zone>
  58. </znode>
  59. </city>
  60. </cnode>
  61. </province>
  62. </pnode>
  63. </root>


解析方法

  1. import java.io.File;
  2. import java.io.IOException;
  3. import javax.xml.parsers.DocumentBuilder;
  4. import javax.xml.parsers.DocumentBuilderFactory;
  5. import javax.xml.parsers.ParserConfigurationException;
  6. import org.w3c.dom.Document;
  7. import org.w3c.dom.Element;
  8. import org.w3c.dom.Node;
  9. import org.w3c.dom.NodeList;
  10. import org.xml.sax.SAXException;
  11.  
  12. /**
  13. * 这里用一句话描述这个类的作用.
  14. *
  15. * @author 杜文俊
  16. */
  17.  
  18. public class newXMLParse {
  19. Document document = null;
  20. NodeList nodeList = null;
  21.  
  22. /**
  23. * 构造函数,初始化Document对象 类的构造方法.
  24. */
  25. public newXMLParse() {
  26. // 指定File文件
  27. File file = new File("E:\\test.xml");
  28. // 建立DocumentBuilderFactory对象
  29. DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  30. DocumentBuilder builder;
  31.  
  32. try {
  33. // 建立DocumentBuilder对象
  34. builder = builderFactory.newDocumentBuilder();
  35. // 用DocumentBuilder对象的parse方法引入文件建立Document对象
  36. document = builder.parse(file);
  37. nodeList = document.getChildNodes();
  38. // province 根据你的xml标签进行解析
  39. NodeList province = document.getElementsByTagName("province");
  40. // 测试1:找出所有province标签,返回NodeList
  41. showByCondition(province);
  42. // 测试2:遍历所有节点
  43. //searchAndShow(nodeList);
  44. } catch (ParserConfigurationException e) {
  45. e.printStackTrace();
  46. } catch (SAXException e) {
  47. e.printStackTrace();
  48. } catch (IOException e) {
  49. System.err.println("找不到你指定的文件!");
  50. e.printStackTrace();
  51. }
  52. }
  53.  
  54. // 测试 1 方法:按条件输出
  55. public void showByCondition(NodeList nodeList) {
  56. System.out.println();
  57. System.out.println("------showByCondition输出结果-------");
  58. Element element;
  59. // 对符合条件的所有节点进行遍历
  60. int nodeListLength = nodeList.getLength();
  61. for (int i = 0; i < nodeList.getLength(); i++) {
  62. // 获得一个元素
  63. element = (Element) nodeList.item(i);
  64. // 此元素有子节点,获取所有子节点,返回一个provinceList
  65. NodeList provinceList = element.getChildNodes();
  66. int provinceListLength = provinceList.getLength();
  67. // 遍历所有子节点
  68. for (int j = 0; j < provinceList.getLength(); j++) {
  69. // 若子节点的名称不为#text,则输出,#text为反/标签
  70. // provinceList.item(j).getNodeName()获取标签的名字
  71. String nodenameStr = provinceList.item(j).getNodeName();
  72. if (!provinceList.item(j).getNodeName().equals("#text")) {
  73. // 输出节点名称、节点值
  74. System.out.println(provinceList.item(j).getNodeName() + ":"+ provinceList.item(j).getTextContent());
  75. }
  76. }
  77. }
  78. }
  79.  
  80. // 测试2 方法:寻找遍历
  81. public void searchAndShow(NodeList nodeList) {
  82. System.out.println();
  83. System.out.println("--------- 接下来让我们看一下解析XML后得到的数据 ---------");
  84. for (int i = 0; i < nodeList.getLength(); i++) {
  85. // 得到一个节点,需要强制转换为Element,这里Node是Element的父类
  86. Node node = nodeList.item(i);
  87. // if (!node.getNodeName().equals("#text")) {
  88. // System.out.println(node.getNodeName());
  89. // }
  90. // System.out.println(element.getAttribute(""));
  91. int childNodesLength = node.getChildNodes().getLength();
  92. if (node.getChildNodes().getLength() > 3) {
  93. // 若包含子节点,则递归遍历
  94. System.out.println("此节点包含子元素!");
  95. searchAndShow(node.getChildNodes());
  96. System.out.println();
  97. } else {
  98. // 若不包含子节点,则输出节点中的内容
  99. if (!node.getTextContent().trim().equals("") && node.getTextContent() != null) {
  100. System.out.println(node.getTextContent());
  101. }
  102. }
  103. }
  104. }
  105.  
  106. /**
  107. * 程序入口
  108. *
  109. * @param args
  110. * @author
  111. * @update 2013-6-3 下午7:29:17
  112. */
  113. public static void main(String[] args) {
  114. new newXMLParse();
  115. }
  116. }

猜你在找的XML相关文章