XPathUtils.java 文件源码

java
阅读 23 收藏 0 点赞 0 评论 0

项目:ARCLib 作者:
/**
 * Searches XML element with XPath and returns list of nodes found
 *
 * @param xml        input stream with the XML in which the element is being searched
 * @param expression XPath expression used in search
 * @return {@link NodeList} of elements matching the XPath in the XML
 * @throws XPathExpressionException     if there is an error in the XPath expression
 * @throws IOException                  if the XML at the specified path is missing
 * @throws SAXException                 if the XML cannot be parsed
 * @throws ParserConfigurationException
 */
public static NodeList findWithXPath(InputStream xml, String expression)
        throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder;

    dBuilder = dbFactory.newDocumentBuilder();

    Document doc = dBuilder.parse(xml);
    doc.getDocumentElement().normalize();

    XPath xPath = XPathFactory.newInstance().newXPath();

    try {
        return (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        throw new InvalidXPathException(expression, e);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号