Java 怎么将XPath转为字符串内容

2019年7月29日20:51:45Java 怎么将XPath转为字符串内容已关闭评论 1,042 576字阅读1分55秒

1.简介

Java 怎么将XPath转为字符串内容?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

 


import javax.xml.xpath.*;
import org.w3c.dom.*;

public class Main {
    public static String xpathToContent(String xpathQuery, Object domObject)
            throws XPathExpressionException {
        Node node = xpathToNode(xpathQuery, domObject);

        if (node != null)
            return node.getTextContent();

        return null;
    }

    public static Node xpathToNode(String xpathQuery, Object domObject)
            throws XPathExpressionException {
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();

        return (Node) xpath.evaluate(xpathQuery, domObject,
                XPathConstants.NODE);
    }
}

 
编程十万个怎么办