Example usage for javax.xml.xpath XPathConstants NODE

List of usage examples for javax.xml.xpath XPathConstants NODE

Introduction

In this page you can find the example usage for javax.xml.xpath XPathConstants NODE.

Prototype

QName NODE

To view the source code for javax.xml.xpath XPathConstants NODE.

Click Source Link

Document

The XPath 1.0 NodeSet data type.

Usage

From source file:Main.java

public static Object parseRequestObjectFromSoap(String soapXml) throws Exception {
    Object result = null;/*from w ww  . ja v  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

/**
 * Returns the XML {@link Element} matching the given XPath expression.
 * @param expression XPath expression./*from   w ww.  j  a  v a 2 s .c  om*/
 * @param document XML document to search for the element.
 * @return The matching XML {@link Element}.
 */
public static Element evaluateXPathAsElement(String expression, Document document) {
    return evaluateXpath(expression, document, XPathConstants.NODE);
}

From source file:Main.java

/**
 * Select only one node what matches given xpath query
 *
 * @param node       xml node/*w w  w  . j  av  a2 s.c  om*/
 * @param expression xpath query
 * @return first element which confirms given xpath query.
 * @throws XPathExpressionException in case of any errors.
 */
public static Element queryElement(final Node node, final String expression) throws XPathExpressionException {
    final XPath xpath = XPathFactory.newInstance().newXPath();
    return (Element) xpath.evaluate(expression, node, XPathConstants.NODE);
}

From source file:Main.java

public static void pokeValue(final Document doc, final String xpathExpression, final String value)
        throws XPathExpressionException {
    final XPath xPath = XPF.newXPath();
    final XPathExpression expression = xPath.compile(xpathExpression);

    final Node node = (Node) expression.evaluate(doc, XPathConstants.NODE);
    // or setValue()?
    node.setTextContent(value);//from  w  w w  .jav  a  2s.  c o m
}

From source file:Main.java

public static Node selectSingleNode(Node node, String express) {
    Node result = null;/*from  w w w  .  j  a v  a  2  s.c o  m*/
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        result = (Node) xpath.evaluate(express, node, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static Object execXpathGetNode(String srcXmlString, String xPath) {
    Object result = null;//from   w ww  .  j a  v  a 2s  .  c  o  m
    try {
        Document doc = stringToDoc(srcXmlString);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(xPath);
        result = expr.evaluate(doc, XPathConstants.NODE);
    } catch (Exception ex) {
        logger.error(ex);
    }
    return result;
}

From source file:Main.java

/** Evaluates an XPath. */
private static synchronized Object evalXPath(String expression, Object item, QName type) {
    if (xPath == null)
        xPath = XPathFactory.newInstance().newXPath();

    try {//  www.j  a  v a  2  s .co m
        XPathExpression exp = xPath.compile(expression);
        Object node = exp.evaluate(item, XPathConstants.NODE);

        return (node == null) ? null : exp.evaluate(item, type);
    } catch (XPathExpressionException e) {
        throw new AssertionError("Error initializing XPath instance for '" + expression + "': " + e);
    }
}

From source file:Main.java

public static Node xpathNode(String expr, Document doc) {
    try {//from w ww  .j ava 2  s  . c o  m
        return (Node) xpath.evaluate(expr, doc, XPathConstants.NODE);
    } catch (Exception e) {
        throw new RuntimeException("Cannot evaluate XPath:", e);
    }
}

From source file:Main.java

/**
 * Evaluates a XPath expression from a XML node, returning a Node object.
 * //www  .  ja  v  a 2s . c  o m
 * @param expression
 *            A XPath expression
 * @param node
 *            The Node for which this expression should be evaluated
 * 
 * @return The result of evaluating the XPath expression to the given Node
 *         or <code>null</code> if an exception occured
 */
public static Node evaluateXPathExpressionAndReturnNode(String expression, Node node) {
    try {
        return (Node) getXPath().get().evaluate(expression, node, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        return null;
    }
}

From source file:Main.java

/**
 * Get the searchHandler Node from the solrconfig.xml file
 *
 * @param solrconfig/*  w  w w  .  j  a  v  a2  s. c om*/
 *            the solrconfig.xml File
 *
 * @return searchHandler XML Node or null if not found
 *
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 * @throws XPathExpressionException
 */
public static Node getSearchHandlerNode(final File solrconfig)
        throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
    final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    final Document docSchem = dBuilder.parse(solrconfig);
    final XPathFactory xPathfactory = XPathFactory.newInstance();
    final XPath xpath = xPathfactory.newXPath();
    final XPathExpression expr = xpath
            .compile("//requestHandler[@class=\"solr.SearchHandler\" and @name=\"/select\"]");
    final Node requestHandler = (Node) expr.evaluate(docSchem, XPathConstants.NODE);
    return requestHandler;
}