zoukankan      html  css  js  c++  java
  • xml解析之dom4j

    xml的解析方法 有2种,Dom 解析  和SAX解析。

    sax是基于事件流的解析,Dom是基于XML文档树结构的解析 。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 
     3     <!-- jdom如何操作xml文件 -->
     4 <person>
     5     <pro>
     6         <id name="id">I08002726</id>
     7         <name>shuanlei</name>
     8         <age name="a">20</age>
     9         <gender>male</gender>
    10         <job>programmer</job>
    11     </pro>
    12 </person>
    XML document文档

    dom4j  解析  xml文档

     1 SAXReader reader = new SAXReader();
     2         //dom4j是用xpath的方式 来进行 解析 地 
     3         try {
     4             //读取XMl文档 document对象  xml  node节点  返回值是element 
     5             Document document = reader.read(new File("src/com/shuanlei/dom4j/hello.xml"));
     6             Element root = document.getRootElement();//拿到xmldocument对象根节点
     7             for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
     8                 Element element = (Element) iterator.next();
     9                 
    10                 
    11                 for (Iterator nodes = element.nodeIterator(); nodes.hasNext();) {
    12                     Node type = (Node) nodes.next();
    13 //                    System.err.println(type.getName()+"--"+type.getText());
    14                 }
    15             }
    View Code

    xpath // 解析 xml文档中 指定 路径的 节点 属性 。 含有完整信息的Node节点 才 叫做 Element。

    需要添加jar.

     1 //xpath  解析 xml  文档中 指定  节点 路径  的 属性 值  节点
     2             //含有完整信息的节点 才算是一个元素
     3             List<Node> List =document.selectNodes("//person/pro");
     4             for (Iterator it = List.iterator(); it.hasNext();) {
     5                 Element node = (Element) it.next();
     6                 for (Iterator its = node.nodeIterator(); its.hasNext();) {
     7                     Node node2 = (Node) its.next();
     8                     System.out.println("ParseXml.read()"+node2.getName());
     9                 }
    10 //            
    11             }
    View Code xml Xpath
  • 相关阅读:
    jquery实现选项卡(两句即可实现)
    常用特效积累
    jquery学习笔记
    idong常用js总结
    织梦添加幻灯片的方法
    LeetCode "Copy List with Random Pointer"
    LeetCode "Remove Nth Node From End of List"
    LeetCode "Sqrt(x)"
    LeetCode "Construct Binary Tree from Inorder and Postorder Traversal"
    LeetCode "Construct Binary Tree from Preorder and Inorder Traversal"
  • 原文地址:https://www.cnblogs.com/shuanlei/p/4244528.html
Copyright © 2011-2022 走看看