Example usage for javax.xml.xpath XPathFactory newXPath

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

Introduction

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

Prototype

public abstract XPath newXPath();

Source Link

Document

Return a new XPath using the underlying object model determined when the XPathFactory was instantiated.

Usage

From source file:Main.java

public static NodeList selectNodes(Node node, String express) {
    NodeList result = null;/*from ww w  .ja  v  a2  s. c  o  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 Node selectSingleNode(Node node, String express) {
    Node result = null;//from   w  w w  .j  a v a  2  s  .  c  om
    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 String getNodeTextByXPath(Element eoEl, String xpath) {
    try {//from  ww w .j  av  a  2  s.com
        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

/**
 * Query xml result./*from   w w w.  j  a  v  a2s  . co  m*/
 * 
 * @param doc
 *            the doc
 * @param xpathExpression
 *            the xpath expression
 * @return the object
 */
public static NodeList queryXMLResult(Document doc, String xpathExpression) {
    // Document doc = getDocument(result);
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    NodeList XmlResult = null;
    try {
        XPathExpression expr = xpath.compile(xpathExpression);
        XmlResult = evaluateXpath(doc, expr);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return XmlResult;
}

From source file:Main.java

public static Object parseRequestObjectFromSoap(String soapXml) throws Exception {
    Object result = null;//from  w  w  w. j av a  2s  .  c o 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 Object getXMLValue(String xml, String xQuery, QName resultType)
        throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(new InputSource(new StringReader(xml)));

    XPathFactory xPathfactory = XPathFactory.newInstance();

    XPath xPath = xPathfactory.newXPath();
    XPathExpression xPathExpression = xPath.compile(xQuery);
    return xPathExpression.evaluate(doc, resultType);
}

From source file:Main.java

/**
 * Get the searchHandler Node from the solrconfig.xml file
 *
 * @param solrconfig//www . j av a 2 s  .com
 *            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;
}

From source file:Main.java

static public XPath getXPath() {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();

    return xpath;
}

From source file:Main.java

/**
 * @return//from  w  w w  .j av  a  2 s  .c  om
 */
public static XPath getHtmlXPath() {
    XPathFactory factory = XPathFactory.newInstance();
    XPath htmlPath = factory.newXPath();
    htmlPath.setNamespaceContext(new NamespaceContext() {
        public String getNamespaceURI(String prefix) {
            return "http://www.w3.org/1999/xhtml";
        }

        public String getPrefix(String namespaceURI) {
            return "h";
        }

        public Iterator<?> getPrefixes(String namespaceURI) {
            List<String> prefixes = new ArrayList<String>();
            prefixes.add("h");

            return prefixes.iterator();
        }
    });

    return htmlPath;
}

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;/*www.  ja v a2s  .c o  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;
}