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

public static String getProcessIdFromBpmn(final String bpmn) {
    String processId = null;//from   w  w w. j ava 2  s. com
    try {
        final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        final Document doc = domFactory.newDocumentBuilder()
                .parse(new ByteArrayInputStream(bpmn.getBytes("UTF-8")));
        final XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(new NamespaceContext() {
            @Override
            public Iterator<?> getPrefixes(final String namespaceURI) {
                // Not used in this context.
                return null;
            }

            @Override
            public String getPrefix(final String namespaceURI) {
                // Not used in this context.
                return null;
            }

            @Override
            public String getNamespaceURI(final String prefix) {
                // Only require the URI for the bpmn2 NS.
                return BPMN2_NAMESPACE_URI;
            }
        });
        final XPathExpression expr = xpath.compile(BPMN_PROCESS_ID_XPATH_EXPR);

        final Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        processId = node.getAttributes().getNamedItem(BPMN_PROCESS_ID_ATTR).getNodeValue();
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return processId;
}

From source file:Main.java

public static NodeList selectNodes(String express, Object source) {
    NodeList result = null;//from ww w  .  j  av  a2s .co  m
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        result = (NodeList) xpath.evaluate(express, source, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static NodeList getNodesByXPath(File file, String xpath) {
    if (!file.isFile() || !file.canRead())
        return null;

    Document document;/*www . j a  v  a  2s.  co  m*/
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    try {
        DocumentBuilder documentBuilder = dbFactory.newDocumentBuilder();
        document = documentBuilder.parse(file);
    } catch (ParserConfigurationException e) {
        return null;
    } catch (SAXException e) {
        return null;
    } catch (IOException e) {
        return null;
    }

    if (document == null) {
        return null;
    }

    Object result;
    try {
        XPath xpathCompiler = XPathFactory.newInstance().newXPath();
        XPathExpression xPathExpr = xpathCompiler.compile(xpath);
        result = xPathExpr.evaluate(document, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        return null;
    }

    return (NodeList) result;
}

From source file:Main.java

public static ArrayList<Node> getNodesXPath(String srcXmlString, String xPath) {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(false); // never forget this!
    Document doc = null;//from  ww w .ja v a2 s  .  co m
    DocumentBuilder builder = null;
    ArrayList<Node> nodesList = new ArrayList<Node>();
    try {
        builder = domFactory.newDocumentBuilder();
        doc = builder.parse(new ByteArrayInputStream(srcXmlString.getBytes()));
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(xPath);

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

        NodeList xPathNodes = (NodeList) result;
        logger.debug("xpath result count: " + xPathNodes.getLength());
        // iterate through all the nodes
        for (int i = 0; i < xPathNodes.getLength(); i++) {
            nodesList.add(xPathNodes.item(i));
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage());
    }
    return nodesList;
}

From source file:Main.java

public static String getNodeTextByXPath(Element eoEl, String xpath) {
    try {// w  w w. j  av  a 2  s. c om
        Document doc = newDocument();
        Element importedEl = (Element) doc.importNode(eoEl.cloneNode(true), true);
        doc.appendChild(importedEl);

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpathEn = factory.newXPath();
        return xpathEn.evaluate(xpath, doc);
    } catch (Exception ex) {
        ex.printStackTrace();
        return "";
    }
}

From source file:Main.java

static void getHTTPXml(URL url) throws Exception {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("ACCEPT", "application/xml");
    InputStream xml = conn.getInputStream();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    org.w3c.dom.Document document = builder.parse(xml);

    System.out.println(document);
    String doctype = conn.getContentType();
    System.out.println(doctype);/* www  .  j a  v a  2s.c o  m*/

    XPathFactory pathFactory = XPathFactory.newInstance();
    XPath path = pathFactory.newXPath();
    XPathExpression expression;
    expression = path.compile("/result/checkid");
    NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
    String checkids[] = getNodeValue(nodeList);
    for (String checkid : checkids) {
        System.out.print(checkid + ", ");
    }
    conn.disconnect();
}

From source file:Main.java

private static ThreadLocal<XPath> getXPath() {
    if (xPath == null) {
        xPath = new ThreadLocal<XPath>() {
            @Override//from   w ww.  j a  v a 2 s  .  c  o  m
            protected XPath initialValue() {
                return XPathFactory.newInstance().newXPath();
            }
        };
    }
    return xPath;
}

From source file:Main.java

public static Object read(Node node, String expression, QName returnType) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression xPathExp = xpath.compile(expression);
    return xPathExp.evaluate(node, returnType);
}

From source file:Main.java

/**
 * Read application-context file, and return fully qualified class name for
 * given <code>beanName</code>
 * /* w w  w  .  j  a  v a  2s  .co m*/
 * @param beanName
 * @return
 * 
 */
public static String getFullyQualifiedClass(String beanName) {

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

    docBuilderFactory.setNamespaceAware(true);

    String nodeValue = "";

    try {
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        Document doc = builder.parse("application-context.xml");

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("//bean[@name='" + beanName + "']/@class");

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

        NodeList nodes = (NodeList) result;
        if (nodes.getLength() > 0) {
            nodeValue = nodes.item(0).getNodeValue();
        }
    } catch (ParserConfigurationException parserConfigurationException) {
        parserConfigurationException.printStackTrace();
    } catch (IOException ioException) {
        ioException.printStackTrace();
    } catch (SAXException saxException) {
        saxException.printStackTrace();
    } catch (XPathExpressionException xPathExpressionException) {
        xPathExpressionException.printStackTrace();
    }

    return nodeValue;
}

From source file:Main.java

public static XPath getSharedXPath() {
    XPath xPath = null;/* w w  w .ja  v a  2s.  c  om*/
    if (sharedXPath != null)
        xPath = sharedXPath.get();
    if (xPath == null) {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        sharedXPath = new SoftReference<XPath>(xPath = xPathFactory.newXPath());
    }
    return xPath;
}