Android XPath Evaluate queryXmlByXPath(Document xmlDocument, String xpathExpressionString)

Here you can find the source of queryXmlByXPath(Document xmlDocument, String xpathExpressionString)

Description

query Xml By X Path

Declaration

public static NodeList queryXmlByXPath(Document xmlDocument,
            String xpathExpressionString) 

Method Source Code

//package com.java2s;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class Main {
    public static NodeList queryXmlByXPath(Document xmlDocument,
            String xpathExpressionString) {
        try {// ww w  .jav  a 2s.co  m

            // Set up xpath query
            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPath xpath = xpathFactory.newXPath();
            XPathExpression xpathExpression = xpath
                    .compile(xpathExpressionString);

            // Evaluate on document and return result
            Object result = xpathExpression.evaluate(xmlDocument,
                    XPathConstants.NODESET);
            return (NodeList) result;
        } catch (XPathExpressionException e) {
            throw new IllegalArgumentException("Not a valid xpath");
        }
    }
}

Related

  1. getElementByXPath(Element e, String xPathExpression)
  2. findXPathNode(Node node, String xPath)
  3. findXPathNodeList(Node node, String xPath)
  4. findChildNodeWithName(Node node, String childName)
  5. getXPathItemText(Document doc, XPath xpath, String query)