Example usage for javax.xml.xpath XPathFactory newInstance

List of usage examples for javax.xml.xpath XPathFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.xpath XPathFactory newInstance.

Prototype

public static XPathFactory newInstance() 

Source Link

Document

Get a new XPathFactory instance using the default object model, #DEFAULT_OBJECT_MODEL_URI , the W3C DOM.

This method is functionally equivalent to:

 newInstance(DEFAULT_OBJECT_MODEL_URI) 

Since the implementation for the W3C DOM is always available, this method will never fail.

Usage

From source file:Main.java

/**
 * Gets the node list from the given xml file and the given xpath expression.
 * //www  .j a  v a  2 s  .  c o m
 * @param xml
 *            the xml
 * @param xpathExpression
 *            the xpath expression
 * @return the node list
 * @throws XPathExpressionException
 *             the x path expression exception
 * @throws ParserConfigurationException
 *             the parser configuration exception
 * @throws SAXException
 *             the sAX exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static NodeList getNodeList(File xml, String xpathExpression)
        throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(xml);
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(xpathExpression);

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    return nodes;
}

From source file:Main.java

private static XPathExpression createXPathExpression(String xpathString) {
    /* XPath *//*from w w  w .j a  v  a 2 s .co m*/
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(new NamespaceContext() {

        @Override
        public Iterator<?> getPrefixes(String namespaceURI) {
            throw new RuntimeException();
        }

        @Override
        public String getPrefix(String namespaceURI) {
            throw new RuntimeException();
        }

        @Override
        public String getNamespaceURI(String prefix) {
            if ("ds".equals(prefix)) {
                return XMLSignature.XMLNS;
            } else if ("xades".equals(prefix)) {
                return "http://uri.etsi.org/01903/v1.3.2#";
            } else if ("xades141".equals(prefix)) {
                return "http://uri.etsi.org/01903/v1.4.1#";
            } else if ("xades111".equals(prefix)) {
                return "http://uri.etsi.org/01903/v1.1.1#";
            }
            throw new RuntimeException("Prefix not recognized : " + prefix);
        }
    });
    try {
        XPathExpression expr = xpath.compile(xpathString);
        return expr;
    } catch (XPathExpressionException ex) {
        throw new RuntimeException(ex);
    }

}

From source file:com.dianping.phoenix.dev.core.tools.scanner.ServiceMetaScanner.java

@Override
protected List<ServicePortEntry> doScan(Document doc) throws Exception {
    List<ServicePortEntry> resList = new ArrayList<ServicePortEntry>();
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//service");

    Object xmlRes = expr.evaluate(doc, XPathConstants.NODESET);

    NodeList nodes = (NodeList) xmlRes;

    for (int i = 0; i < nodes.getLength(); i++) {
        String serviceName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
        XPathExpression portExpr = xpath.compile("//service[@name='" + serviceName + "']/port/text()");
        Object portRes = portExpr.evaluate(doc, XPathConstants.NODESET);
        NodeList ports = (NodeList) portRes;
        int projectPort = -1;
        if (ports.getLength() >= 1) {
            projectPort = Integer.parseInt(ports.item(0).getNodeValue());
        }/*from w  ww . java2s.  co  m*/

        if (projectPort > 0 && StringUtils.isNotBlank(serviceName)) {
            resList.add(new ServicePortEntry(serviceName, projectPort));
        }
    }

    return resList;
}

From source file:com.dianping.maven.plugin.tools.misc.scanner.ProjectMetaScanner.java

@Override
protected List<ProjectPortEntry> doScan(Document doc) throws Exception {
    List<ProjectPortEntry> resList = new ArrayList<ProjectPortEntry>();
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//project");

    Object xmlRes = expr.evaluate(doc, XPathConstants.NODESET);

    NodeList nodes = (NodeList) xmlRes;

    for (int i = 0; i < nodes.getLength(); i++) {
        String projectName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
        XPathExpression portExpr = xpath.compile("//project[@name='" + projectName + "']/port/text()");
        Object portRes = portExpr.evaluate(doc, XPathConstants.NODESET);
        NodeList ports = (NodeList) portRes;
        int projectPort = -1;
        if (ports.getLength() >= 1) {
            projectPort = Integer.parseInt(ports.item(0).getNodeValue());
        }//from   w  w  w. ja va 2 s . c  o m

        if (projectPort > 0 && StringUtils.isNotBlank(projectName)) {
            resList.add(new ProjectPortEntry(projectName, projectPort));
        }
    }

    return resList;
}

From source file:Main.java

public static XPathFactory newXPathFactory() {
    return XPathFactory.newInstance();
}

From source file:com.alliander.osgp.platform.cucumber.RunXpathResult.java

public XpathResult runXPathExpression(final String response, final String path)
        throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(response));
    final Document doc = builder.parse(is);
    final XPathFactory xPathfactory = XPathFactory.newInstance();
    final XPath xpath = xPathfactory.newXPath();

    return new XpathResult(xpath.compile(path), doc);
}

From source file:Main.java

private static XPathFactory createFactory() {
    return XPathFactory.newInstance();
}

From source file:Main.java

/**
 * Returns a compiled XPath expression./*from ww  w . j  a va 2 s  . c  o m*/
 *
 * @param expression                The XPath expression as a String.
 * @param namespaceContext          The namespace context used by the XPath expression.
 * @return                          The compiled XPathExpression.
 * @throws XPathExpressionException If a parsing error occurs.
 */
public static XPathExpression compile(String expression, NamespaceContext namespaceContext)
        throws XPathExpressionException {
    XPath compiler = XPathFactory.newInstance().newXPath();
    if (namespaceContext != null)
        compiler.setNamespaceContext(namespaceContext);
    return compiler.compile(expression);
}

From source file:Main.java

public static NodeList getNodeList(Node node, String path) {
    try {/*  w w  w .j  a  v a  2  s.com*/
        slowGetValue++;
        // System.out.println("slow : " + slowGetValue + " - " + path);

        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        XPathExpression expr;
        expr = xpath.compile(path);
        NodeList list = (NodeList) expr.evaluate(node, XPathConstants.NODESET);

        return list;
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:net.javacrumbs.springws.test.common.XPathExpressionEvaluator.java

public String evaluateExpression(Document document, String expression, URI uri,
        NamespaceContext namespaceContext) {
    XPathFactory factory = XPathFactory.newInstance();
    factory.setXPathVariableResolver(new WsTestXPathVariableResolver(uri));
    try {/*from   w  w w .ja va 2 s  .  c  o  m*/
        XPath xpath = factory.newXPath();
        if (namespaceContext != null) {
            xpath.setNamespaceContext(namespaceContext);
        }
        String result = xpath.evaluate(expression, document);
        logger.debug("Expression \"" + expression + "\" resolved to \"" + result + "\"");
        return result;
    } catch (XPathExpressionException e) {
        throw new ExpressionResolverException(
                "Could not evaluate XPath expression \"" + expression + "\" : \"" + e.getMessage() + "\"", e);
    }
}