现在的位置: 首页 > 综合 > 正文

关于XmlUtils类(1)

2016年08月17日 ⁄ 综合 ⁄ 共 2773字 ⁄ 字号 评论关闭

package com.xiao.demo.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
 * xml常用工具类
 */
public class XmlUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(XmlUtils.class);

    public static NodeList runXpath(Node doc, String xquery, NamespaceContext ns) {
        NodeList list = null;
        try {
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            if (null != ns) {
                xpath.setNamespaceContext(ns);
            }
            XPathExpression expression = xpath.compile(xquery);

            Object result = expression.evaluate(doc, XPathConstants.NODESET);
            list = (NodeList) result;
        } catch (Exception e) {
            LOGGER.warn("XMLUtils:  unable to evaluate xpath", e);
            LOGGER.error("", e);
        }
        return list;
    }

    public  static  String getValueByXpath(Node doc, String xquery) {
        try {
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expression = xpath.compile(xquery);
            String result = (String) expression.evaluate(doc, XPathConstants.STRING);
            return result;
        } catch (Exception e) {
            LOGGER.error("", e);
            LOGGER.warn("XMLUtils:  unable to evaluate xpath", e);
        }
        return null;
    }

    public static String nodeAsString(Node node) {
        String nodeStr = "";
        TransformerFactory tff = TransformerFactory.newInstance();
        try {
            Transformer tf = tff.newTransformer();
            tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            tf.transform(new DOMSource(node), new StreamResult(bos));
            return bos.toString("UTF-8");
        } catch (Exception e) {
            LOGGER.warn("XMLUtils#nodeAsString: ", e);
            LOGGER.error("", e);
        }

        return nodeStr;
    }

    public static Document getW3CDom(String xmlStr) throws ParserConfigurationException,
            SAXException, IOException {
        StringReader sr = new StringReader(xmlStr);
        InputSource is = new InputSource(sr);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(is);

        return doc;
    }

}

抱歉!评论已关闭.