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 Object parseRequestObjectFromSoap(String soapXml) throws Exception {
    Object result = null;//from ww  w . jav  a2s .  co  m

    if (soapXml != null && soapXml.trim().length() > 0) {
        DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
        xmlFact.setNamespaceAware(true);

        DocumentBuilder builder = xmlFact.newDocumentBuilder();
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        StringReader xsr = new StringReader(soapXml);
        InputSource is = new InputSource(xsr);
        Document doc = builder.parse(is);

        //Node requestNode = (Node) xpath.evaluate("/soap:Envelope/soap:Body/*", doc, XPathConstants.NODE);
        Node requestNode = (Node) xpath.evaluate("/*[local-name()='Envelope']/*[local-name()='Body']/*", doc,
                XPathConstants.NODE);

        JAXBContext ctx = JAXBContext.newInstance("xo.xoi.orderentry.ebonding");
        Unmarshaller unmarshaller = ctx.createUnmarshaller();
        result = unmarshaller.unmarshal(requestNode);
    }

    return result;
}

From source file:Main.java

public static XPathExpression createXPathExpression(NamespaceContext context, String xPathQuery)
        throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(context);//from   w  w w.j  a v  a2 s. c om
    return xpath.compile(xPathQuery);
}

From source file:Main.java

public static XPathExpression getXPathExpression(String path) throws RuntimeException {
    path = path.trim();/*from   ww w. j ava 2  s .  com*/
    try {
        XPath xp = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xp.compile(path);
        return expr;

    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

}

From source file:Main.java

/**
 * Get node from XML document using xpath expression
 * //from  w  ww .j  a  va2s  . c o  m
 * @param doc
 * @param expression
 * @return
 * @throws XPathExpressionException
 */
public static Node getNode(final Document doc, final String expression) throws XPathExpressionException {
    if (null == expression || expression.isEmpty() || null == doc) {
        return null;
    }
    log.finer("Executing xpath xpression " + expression);
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xPath.compile(expression);
    Object result = expr.evaluate(doc, XPathConstants.NODE);
    return (Node) result;
}

From source file:Main.java

/**
 * Get one element from XML document using xpath expression
 * /*w w  w .j a  va  2  s  .  com*/
 * @param doc
 * @param expression
 * @return
 * @throws XPathExpressionException
 */
public static Element getElement(final Document doc, final String expression) throws XPathExpressionException {
    if (null == expression || expression.isEmpty() || null == doc) {
        return null;
    }
    log.finer("Executing xpath xpression " + expression);
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xPath.compile(expression);
    Object result = expr.evaluate(doc, XPathConstants.NODE);
    return (Element) result;
}

From source file:Main.java

public static String getQuery(String id) throws Exception {
    InputStream is = new ByteArrayInputStream(xml.getBytes("UTF8"));
    InputSource inputSource = new InputSource(is);
    XPath xpath = XPathFactory.newInstance().newXPath();
    return xpath.evaluate("/queries/query[@id='" + id + "']", inputSource);
}

From source file:Main.java

public static List<Node> xpathToNodeList(String xpathQuery, Object domObject) throws XPathExpressionException {
    List<Node> result = new ArrayList<Node>();

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    NodeList nodeList = (NodeList) xpath.evaluate(xpathQuery, domObject, XPathConstants.NODESET);

    for (int i = 0; i < nodeList.getLength(); i++)
        result.add(nodeList.item(i));/*from   www  . j  a  v  a2s .co m*/

    return result;
}

From source file:Main.java

/**
 * Get nodelist from XML document using xpath expression
 * // w w w  .jav  a 2  s  .  c  om
 * @param doc
 * @param expression
 * @return
 * @throws XPathExpressionException
 */
public static NodeList getNodeList(final Document doc, final String expression)
        throws XPathExpressionException {
    if (null == expression || expression.isEmpty() || null == doc) {
        return null;
    }
    log.finer("Executing xpath xpression " + expression);
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xPath.compile(expression);
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    return (NodeList) result;
}

From source file:Main.java

public static NodeList selectNodes(Node node, String express) {
    NodeList result = null;/*from  ww  w  .  ja  v  a  2  s  . co  m*/
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        result = (NodeList) xpath.evaluate(express, node, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static XPathExpression compileXPathExpression(String xPathExpression) {

    final XPathFactory factory = XPathFactory.newInstance();
    final XPath xpath = factory.newXPath();
    try {/*  w w  w  . j  a v  a 2 s  . co m*/
        return xpath.compile(xPathExpression);
    } catch (XPathExpressionException e) {
        throw new RuntimeException("Internal error, invalid XPATH expression " + xPathExpression, e);
    }
}