java使用dom4j解析xml

目标:将指定XML进行解析,以键=值的方式返回指定节点下的数据

所需要的jar包:dom4j1.6.1.jar、jaxen-1.1.jar

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SOAP-ENV:Body>
        <m:Main xmlns:m="http://webservice.xnhdbbx.org/">
            <arg0>
                <![CDATA[
                    <business>
                        <functioncode>400105</functioncode>
                        <d401_19>411102</d401_19>
                        <t104_03>1</t104_03>
                        <list>
                            <item id='1'>
                                <i201_00>FDAC4FC422F5E339E04000</i201_00>
                            </item>
                        </list>
                        <remark1 />
                        <remark2 />
                        <remark3 />
                        <remark4 />
                        <remark5 />
                    </business>
                ]]>
            </arg0>
        </m:Main>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
View Code

如上的xml,我的目的是获得item下节点数据。格式为:i201_00=FDAC4FC422F5E339E04000

下面是代码

package cn.code;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class Dom4JParseXml {
    public static void main(String[] args) {
        SAXReader reader = new SAXReader();
        File file = new File("in.xml");
        try {
            Document doc = reader.read(file);
            Element root = doc.getRootElement();
            String nodePath = "/business/list/item";
            System.out.println(bar(root, nodePath));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    // 遍历root 直到它的下一个节点为Node
    public static String bar(Element root, String nodePath)
            throws DocumentException {
        Iterator i = root.elementIterator();
        Element element = null;
        while (i.hasNext()) {
            element = (Element) i.next();
            if (element.elementIterator().hasNext()) {
                return bar(element, nodePath);
            } else {
                return barNode(element, nodePath);
            }
        }
        return null;
    }

    // 遍历element下的Node
    public static String barNode(Node node, String nodePath) {
        StringBuffer buf = new StringBuffer();
        try {
            Document document = DocumentHelper.parseText(node.getStringValue());
            List list1 = document.selectNodes(nodePath);
            for (Object object : list1) {
                Element n = (Element) object;
                List i201_ = n.elements();
                for (Object object2 : i201_) {
                    Node i_node = (Node) object2;
                    buf.append(i_node.getName() + "="
                            + i_node.getStringValue().trim());
                }
            }
        } catch (Exception e) {
            System.out.println("node.getStringValue() parseText Exception");
        }
        return buf.toString();
    }
}
View Code

上面是完整的代码。

注意以上的XML中,element arg0下面的数据是通过<![CDATA[..]]>包围的,<![CDATA[..]]>中的文本解释器是不会执行的(会被解析器忽略),那么从这可以知道arg0是一个节点的元素(element),而<![CDATA[..]]>里面的内容只是个纯文本.所以在bar这个方法中用到了迭代,主要是将纯文本拿到。

第二,由纯文本的结构可知,该文本是一个document,故在barNode这个方法里,首先将这个文本解析成一个document.然后调用document.selectNodes("");方法得到一个Node的集合,再进行遍历.  其中document还有document.selectSingleNode("")方法,这个方法是直接得到一个Node节点.

参考资料:http://dom4j.sourceforge.net/dom4j-1.6.1/guide.html

原文地址:https://www.cnblogs.com/a-really/p/3920728.html