• dom4j解析xml字符串实例


    DOM4J

        与利用DOM、SAX、JAXP机制来解析xml相比,DOM4J 表现更优秀,具有性能优异、功能强大和极端易用使用的特点,只要懂得DOM基本概念,就可以通过dom4j的api文档来解析xml。dom4j是一套开源的api。实际项目中,往往选择dom4j来作为解析xml的利器。

    先来看看dom4j中对应XML的DOM树建立的继承关系

      

    针对于XML标准定义,对应于图2-1列出的内容,dom4j提供了以下实现:

      

    同时,dom4j的NodeType枚举实现了XML规范中定义的node类型。如此可以在遍历xml文档的时候通过常量来判断节点类型了。

     

    常用API

    class org.dom4j.io.SAXReader

    • read  提供多种读取xml文件的方式,返回一个Domcument对象

    interface org.dom4j.Document

    • iterator  使用此法获取node
    • getRootElement  获取根节点

    interface org.dom4j.Node

    • getName  获取node名字,例如获取根节点名称为bookstore
    • getNodeType  获取node类型常量值,例如获取到bookstore类型为1——Element
    • getNodeTypeName  获取node类型名称,例如获取到的bookstore类型名称为Element

    interface org.dom4j.Element

    • attributes  返回该元素的属性列表
    • attributeValue  根据传入的属性名获取属性值
    • elementIterator  返回包含子元素的迭代器
    • elements  返回包含子元素的列表

    interface org.dom4j.Attribute

    • getName  获取属性名
    • getValue  获取属性值

    interface org.dom4j.Text

    • getText  获取Text节点值

    interface org.dom4j.CDATA

    • getText  获取CDATA Section值

    interface org.dom4j.Comment

    • getText  获取注释 

     

    实例一:

    复制代码
    //先加入dom4j.jar包 
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    

    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;

    /**

    • @Title: TestDom4j.java

    • @Package

    • @Description: 解析xml字符串

    • @author 无处不在

    • @date 2012-11-20 下午05:14:05

    • @version V1.0
      */
      public class TestDom4j {

      public void readStringXml(String xml) {
      Document doc
      = null;
      try {

            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 读取并解析XML文档
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> SAXReader就是一个管道,用一个流的方式,把xml文件读出来
            </span><span style="color: #008000;">//</span> 
            <span style="color: #008000;">//</span><span style="color: #008000;"> SAXReader reader = new SAXReader(); </span><span style="color: #008000;">//</span><span style="color: #008000;">User.hbm.xml表示你要解析的xml文档
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> Document document = reader.read(new File("User.hbm.xml"));
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 下面的是通过解析xml字符串的</span>
            doc = DocumentHelper.parseText(xml); <span style="color: #008000;">//</span><span style="color: #008000;"> 将字符串转为XML</span>
      
    Element rootElt = doc.getRootElement(); // 获取根节点 System.out.println("根节点:" + rootElt.getName()); // 拿到根节点的名称 Iterator iter = rootElt.elementIterator("head"); // 获取根节点下的子节点head
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 遍历head节点</span>
            <span style="color: #0000ff;">while</span><span style="color: #000000;"> (iter.hasNext()) {
    
                Element recordEle </span>=<span style="color: #000000;"> (Element) iter.next();
                String title </span>= recordEle.elementTextTrim("title"); <span style="color: #008000;">//</span><span style="color: #008000;"> 拿到head节点下的子节点title值</span>
                System.out.println("title:" +<span style="color: #000000;"> title);
    
                Iterator iters </span>= recordEle.elementIterator("script"); <span style="color: #008000;">//</span><span style="color: #008000;"> 获取子节点head下的子节点script
    
                </span><span style="color: #008000;">//</span><span style="color: #008000;"> 遍历Header节点下的Response节点</span>
                <span style="color: #0000ff;">while</span><span style="color: #000000;"> (iters.hasNext()) {
    
                    Element itemEle </span>=<span style="color: #000000;"> (Element) iters.next();
    
                    String username </span>= itemEle.elementTextTrim("username"); <span style="color: #008000;">//</span><span style="color: #008000;"> 拿到head下的子节点script下的字节点username的值</span>
                    String password = itemEle.elementTextTrim("password"<span style="color: #000000;">);
    
                    System.out.println(</span>"username:" +<span style="color: #000000;"> username);
                    System.out.println(</span>"password:" +<span style="color: #000000;"> password);
                }
            }
            Iterator iterss </span>= rootElt.elementIterator("body"); <span style="color: #008000;">//</span><span style="color: #008000;">/获取根节点下的子节点body
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 遍历body节点</span>
            <span style="color: #0000ff;">while</span><span style="color: #000000;"> (iterss.hasNext()) {
    
                Element recordEless </span>=<span style="color: #000000;"> (Element) iterss.next();
                String result </span>= recordEless.elementTextTrim("result"); <span style="color: #008000;">//</span><span style="color: #008000;"> 拿到body节点下的子节点result值</span>
                System.out.println("result:" +<span style="color: #000000;"> result);
    
                Iterator itersElIterator </span>= recordEless.elementIterator("form"); <span style="color: #008000;">//</span><span style="color: #008000;"> 获取子节点body下的子节点form
                </span><span style="color: #008000;">//</span><span style="color: #008000;"> 遍历Header节点下的Response节点</span>
                <span style="color: #0000ff;">while</span><span style="color: #000000;"> (itersElIterator.hasNext()) {
    
                    Element itemEle </span>=<span style="color: #000000;"> (Element) itersElIterator.next();
    
                    String banlce </span>= itemEle.elementTextTrim("banlce"); <span style="color: #008000;">//</span><span style="color: #008000;"> 拿到body下的子节点form下的字节点banlce的值</span>
                    String subID = itemEle.elementTextTrim("subID"<span style="color: #000000;">);
    
                    System.out.println(</span>"banlce:" +<span style="color: #000000;"> banlce);
                    System.out.println(</span>"subID:" +<span style="color: #000000;"> subID);
                }
            }
        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (DocumentException e) {
            e.printStackTrace();
    
        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
            e.printStackTrace();
    
        }
    }
    
    </span><span style="color: #008000;">/**</span><span style="color: #008000;">
     * @description 将xml字符串转换成map
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> xml
     * </span><span style="color: #808080;">@return</span><span style="color: #008000;"> Map
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> Map readStringXmlOut(String xml) {
        Map map </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> HashMap();
        Document doc </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 将字符串转为XML</span>
            doc =<span style="color: #000000;"> DocumentHelper.parseText(xml); 
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取根节点</span>
            Element rootElt =<span style="color: #000000;"> doc.getRootElement(); 
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 拿到根节点的名称</span>
            System.out.println("根节点:" +<span style="color: #000000;"> rootElt.getName()); 
    
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取根节点下的子节点head</span>
            Iterator iter = rootElt.elementIterator("head"<span style="color: #000000;">); 
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 遍历head节点</span>
            <span style="color: #0000ff;">while</span><span style="color: #000000;"> (iter.hasNext()) {
    
                Element recordEle </span>=<span style="color: #000000;"> (Element) iter.next();
                </span><span style="color: #008000;">//</span><span style="color: #008000;"> 拿到head节点下的子节点title值</span>
                String title = recordEle.elementTextTrim("title"<span style="color: #000000;">); 
                System.out.println(</span>"title:" +<span style="color: #000000;"> title);
                map.put(</span>"title"<span style="color: #000000;">, title);
                </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取子节点head下的子节点script</span>
                Iterator iters = recordEle.elementIterator("script"<span style="color: #000000;">); 
                </span><span style="color: #008000;">//</span><span style="color: #008000;"> 遍历Header节点下的Response节点</span>
                <span style="color: #0000ff;">while</span><span style="color: #000000;"> (iters.hasNext()) {
                    Element itemEle </span>=<span style="color: #000000;"> (Element) iters.next();
                    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 拿到head下的子节点script下的字节点username的值</span>
                    String username = itemEle.elementTextTrim("username"<span style="color: #000000;">); 
                    String password </span>= itemEle.elementTextTrim("password"<span style="color: #000000;">);
    
                    System.out.println(</span>"username:" +<span style="color: #000000;"> username);
                    System.out.println(</span>"password:" +<span style="color: #000000;"> password);
                    map.put(</span>"username"<span style="color: #000000;">, username);
                    map.put(</span>"password"<span style="color: #000000;">, password);
                }
            }
    
            </span><span style="color: #008000;">//</span><span style="color: #008000;">获取根节点下的子节点body</span>
            Iterator iterss = rootElt.elementIterator("body"<span style="color: #000000;">); 
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 遍历body节点</span>
            <span style="color: #0000ff;">while</span><span style="color: #000000;"> (iterss.hasNext()) {
                Element recordEless </span>=<span style="color: #000000;"> (Element) iterss.next();
                </span><span style="color: #008000;">//</span><span style="color: #008000;"> 拿到body节点下的子节点result值</span>
                String result = recordEless.elementTextTrim("result"<span style="color: #000000;">); 
                System.out.println(</span>"result:" +<span style="color: #000000;"> result);
                </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取子节点body下的子节点form</span>
                Iterator itersElIterator = recordEless.elementIterator("form"<span style="color: #000000;">); 
                </span><span style="color: #008000;">//</span><span style="color: #008000;"> 遍历Header节点下的Response节点</span>
                <span style="color: #0000ff;">while</span><span style="color: #000000;"> (itersElIterator.hasNext()) {
                    Element itemEle </span>=<span style="color: #000000;"> (Element) itersElIterator.next();
                    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 拿到body下的子节点form下的字节点banlce的值</span>
                    String banlce = itemEle.elementTextTrim("banlce"<span style="color: #000000;">); 
                    String subID </span>= itemEle.elementTextTrim("subID"<span style="color: #000000;">);
    
                    System.out.println(</span>"banlce:" +<span style="color: #000000;"> banlce);
                    System.out.println(</span>"subID:" +<span style="color: #000000;"> subID);
                    map.put(</span>"result"<span style="color: #000000;">, result);
                    map.put(</span>"banlce"<span style="color: #000000;">, banlce);
                    map.put(</span>"subID"<span style="color: #000000;">, subID);
                }
            }
        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (DocumentException e) {
            e.printStackTrace();
        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
            e.printStackTrace();
        }
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> map;
    }
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
    
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 下面是需要解析的xml字符串例子</span>
        String xmlString = "&lt;html&gt;" + "&lt;head&gt;" + "&lt;title&gt;dom4j解析一个例子&lt;/title&gt;"
                + "&lt;script&gt;" + "&lt;username&gt;yangrong&lt;/username&gt;"
                + "&lt;password&gt;123456&lt;/password&gt;" + "&lt;/script&gt;" + "&lt;/head&gt;"
                + "&lt;body&gt;" + "&lt;result&gt;0&lt;/result&gt;" + "&lt;form&gt;"
                + "&lt;banlce&gt;1000&lt;/banlce&gt;" + "&lt;subID&gt;36242519880716&lt;/subID&gt;"
                + "&lt;/form&gt;" + "&lt;/body&gt;" + "&lt;/html&gt;"<span style="color: #000000;">;
    
        </span><span style="color: #008000;">/*</span><span style="color: #008000;">
         * Test2 test = new Test2(); test.readStringXml(xmlString);
         </span><span style="color: #008000;">*/</span><span style="color: #000000;">
        Map map </span>=<span style="color: #000000;"> readStringXmlOut(xmlString);
        Iterator iters </span>=<span style="color: #000000;"> map.keySet().iterator();
        </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (iters.hasNext()) {
            String key </span>= iters.next().toString(); <span style="color: #008000;">//</span><span style="color: #008000;"> 拿到键</span>
            String val = map.get(key).toString(); <span style="color: #008000;">//</span><span style="color: #008000;"> 拿到值</span>
            System.out.println(key + "=" +<span style="color: #000000;"> val);
        }
    }
    

    }

    复制代码

    实例二:

    复制代码
    /**
     * 解析包含有DB连接信息的XML文件
     * 格式必须符合如下规范:
     * 1. 最多三级,每级的node名称自定义;
     * 2. 二级节点支持节点属性,属性将被视作子节点;
     * 3. CDATA必须包含在节点中,不能单独出现。
     *
     * 示例1——三级显示:
     * <db-connections>
     *         <connection>
     *            <name>DBTest</name>
     *            <jndi></jndi>
     *            <url>
     *                <![CDATA[jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=UTF8]]>
     *             </url>
     *            <driver>org.gjt.mm.mysql.Driver</driver>
     *             <user>test</user>
     *            <password>test2012</password>
     *            <max-active>10</max-active>
     *            <max-idle>10</max-idle>
     *            <min-idle>2</min-idle>
     *            <max-wait>10</max-wait>
     *            <validation-query>SELECT 1+1</validation-query>
     *         </connection>
     * </db-connections>
     *
     * 示例2——节点属性:
     * <bookstore>
     *         <book category="cooking">
     *            <title lang="en">Everyday Italian</title>
     *            <author>Giada De Laurentiis</author>
     *            <year>2005</year>
     *            <price>30.00</price>
     *         </book>
     *
     *         <book category="children" title="Harry Potter" author="J K. Rowling" year="2005" price="$29.9"/>
     * </bookstore>
    
    复制代码
    复制代码
     *
     * @param configFile
     * @return
     * @throws Exception
     */
    public static List<Map<String, String>> parseDBXML(String configFile) throws Exception {
        List<Map<String, String>> dbConnections = new ArrayList<Map<String, String>>();
        InputStream is = Parser.class.getResourceAsStream(configFile);
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(is);
        Element connections = document.getRootElement();
    
    Iterator</span>&lt;Element&gt; rootIter =<span style="color: #000000;"> connections.elementIterator();
    </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (rootIter.hasNext()) {
        Element connection </span>=<span style="color: #000000;"> rootIter.next();
        Iterator</span>&lt;Element&gt; childIter =<span style="color: #000000;"> connection.elementIterator();
        Map</span>&lt;String, String&gt; connectionInfo = <span style="color: #0000ff;">new</span> HashMap&lt;String, String&gt;<span style="color: #000000;">();
        List</span>&lt;Attribute&gt; attributes =<span style="color: #000000;"> connection.attributes();
        </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; attributes.size(); ++i) { <span style="color: #008000;">//</span><span style="color: #008000;"> 添加节点属性</span>
    

    connectionInfo.put(attributes.get(i).getName(), attributes.get(i).getValue());
    }
    while (childIter.hasNext()) { // 添加子节点
    Element attr = childIter.next();
    connectionInfo.put(attr.getName().trim(), attr.getText().trim());
    }
    dbConnections.add(connectionInfo);
    }

    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> dbConnections;
    

    }

    复制代码
  • 相关阅读:
    原型设计作业
    案例分析作业
    202103226-1 编程作业
    准备工作
    通读《构建之法》
    顺序栈的基本操作
    原型设计(图书馆小程序)
    案例分析
    词频统计
    写出这个数
  • 原文地址:https://www.cnblogs.com/jpfss/p/9773834.html
Copyright © 2020-2023  润新知