Java XPath Select selectXPathString(final String xPath, final Node inNode, final String nsuri, final String pre)

Here you can find the source of selectXPathString(final String xPath, final Node inNode, final String nsuri, final String pre)

Description

select X Path String

License

Apache License

Declaration

public static String selectXPathString(final String xPath, final Node inNode, final String nsuri,
            final String pre) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.xml.namespace.QName;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;

public class Main {
    public static final String XPATH_FACTORY = "net.sf.saxon.xpath.XPathFactoryImpl";

    public static String selectXPathString(final String xPath, final Node inNode, final String nsuri,
            final String pre) throws Exception {
        return (String) getNodesListXpath(xPath, inNode, nsuri, pre, XPathConstants.STRING);
    }//from w  w  w  . j  a  v  a  2 s . co  m

    public static String selectXPathString(final String xPath, final Node inNode) throws Exception {
        return selectXPathString(xPath, inNode, "", "");
    }

    /**
     * 
     * @param xPathS
     * @param node
     * @param nsuri
     * @param pre
     * @param returnType
     * @return Return type is one of XPathConstants .BOOLEAN, .NODE, .NODESET,
     *         .NUMBER, .STRING
     * @throws Exception
     */
    public static Object getNodesListXpath(final String xPathS, final Node node, final String nsuri,
            final String pre, final QName returnType) throws Exception {
        Object matches = null;
        System.setProperty("javax.xml.xpath.XPathFactory:" + XPathConstants.DOM_OBJECT_MODEL, XPATH_FACTORY);

        XPathFactory xpathFactory = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL);
        XPath xpath = xpathFactory.newXPath();
        XPathExpression xpe = xpath.compile(xPathS);
        matches = xpe.evaluate(node, returnType);

        return matches;
    }
}

Related

  1. selectSingleNode(final Node node, final String xPath)
  2. selectSingleNode(final Node sourceNode, final String xPathExpression)
  3. selectSingleNode(Node node, String xpath, NamespaceContext context)
  4. selectString(String xpath, Object node)
  5. selectStrings(String xpath, Object node)
  6. xmlSelectNodes(Node node, String xpathExpression)
  7. XPathAPI_selectNodeList(Document doc, String xpath, Node namespaceNode)
  8. xpathNode(String expr, Document doc)
  9. xpathNodeList(String expr, Document doc)