soap协议调用返回的xml解析

工具类
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.xml.sax.InputSource;

@Component
public class Dom4jUtil {

    private  Document document = null;
    private  Element root = null;
    private static Logger logger = LoggerFactory.getLogger(Dom4jUtil.class);

    /**
     * 解析xml,目前采用dom4j进行解析
     */
    public void parseXml(String inXml) throws Exception {
        if (inXml == null || inXml.equals("")) {
            throw new Exception("xml is null or the content of xml is null");
        }
        try {
            SAXReader reader = new SAXReader();
            reader.setValidation(false);
            reader.setEntityResolver(new IgnoreDTDEntityResolver());
            InputSource source = new InputSource(new StringReader(inXml));
            document = reader.read(source);
            root = document.getRootElement();
        } catch (DocumentException e) {
            logger.info("Dom4jUtil.parseXml--发生异常:" + e.getMessage());
        }
    }

    /**
     * 通过xpath路径取得特定节点的内容
     */
    public String getValueXPath(String xpath) {
        Node node = getNodeXPath(xpath);
        if (node != null) {
            return node.getText();
        } else {
            return null;
        }
    }

    public Node getNodeXPath(String xpath) {
        if (root == null) {
            root = document.getRootElement();
        }
        Node node = root.selectSingleNode(xpath);
        return node;
    }

    public List getNodesXPath(String xpath) {
        if (root == null) {
            root = document.getRootElement();
        }
        return root.selectNodes(xpath);
    }

    public String getValueXPath(String xpath, int i) {
        try {
            Node node = this.getNodesXPath(xpath, i);
            if (node != null) {
                return node.getText();
            }
        } catch (Exception e) {
            logger.info("Dom4jUtil.getValueXPath--发生异常:"+e.getMessage());
        }
        return null;
    }

    public Node getNodesXPath(String xpath, int i) {
        return (Node) this.getNodesXPath(xpath).get(i);
    }

    /**
     * dig为true,返回xpath所指节点的所有子节点;dig为false,返回所有xpath所指节点
     */
    @SuppressWarnings("unchecked")
    public List<HashMap<String, String>> getValuesXPath(String xpath, boolean dig) {
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        if (root == null) {
            root = document.getRootElement();
        }
        List<Node> nodeList = root.selectNodes(xpath);
        for (Node tmpNode : nodeList) {
            HashMap map = new HashMap();
            if (dig) {
                if (tmpNode instanceof Element) {
                    Element tmpElement = (Element) tmpNode;
                    for (Iterator i = tmpElement.elementIterator(); i.hasNext(); ) {
                        Element element = (Element) i.next();
                        map.put(element.getName(), element.getText());
                    }
                }
            } else {
                map.put(tmpNode.getName(), tmpNode.getText());
            }
            list.add(map);
        }
        return list;
    }

    //2017-11-22新加的
    public int getSize(String xpath) {
        List list = this.getNodesXPath(xpath);
        if (list != null)
            return list.size();
        return 0;
    }

    public void clear() {
        if (document != null) {
            document.clearContent();
            root = null;
        }
    }
public List getNodesXPathNew(String xpath) {
        root = document.getRootElement();
        return root.selectNodes(xpath);
    }
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * @Describe: 取消dom4j解析dtd文件
 * @Date 9:28 2017/11/15
 **/
public class IgnoreDTDEntityResolver implements EntityResolver {
    @Override
    public InputSource resolveEntity(String publicId, String systemId)
            throws SAXException, IOException {
        return new InputSource(new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
    }
}


另外我还用到了Node节点的一些方法(注意Node是org.dom4j.Node)

node.selectSingleNode("当前node节点下的节点名称");

node.selectNodes("用一个 XPath 表达式查询选择节点。")


参考:XPATH语法      http://www.w3school.com.cn/xpath/xpath_syntax.asp

          Node对象属性以及方法     http://www.w3school.com.cn/xmldom/dom_node.asp


阅读更多
文章标签: dom4j
个人分类: dom4j
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭