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 XPath getXpath() {
    XPathFactory newInstance = XPathFactory.newInstance();
    return newInstance.newXPath();
}

From source file:Main.java

/**
 * @param node  An XML DOM Tree for query
 * @param query An XPATH query to run against the DOM Tree
 * @param nc    The namespaceContext that maps prefixes to XML namespace
 * @return A list of nodes that result from running the query against
 *         the node./*  w w  w  .j a  v  a  2  s  .c om*/
 * @throws Exception If anything goes wrong. No error handling for brevity
 */
public static NodeList query(Node node, String query, NamespaceContext nc) throws Exception {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    xpath.setNamespaceContext(nc);
    return (NodeList) xpath.evaluate(query, node, XPathConstants.NODESET);
}

From source file:Main.java

public static NodeList getNodes(Node doc, String xpath_url) throws XPathExpressionException {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    return (NodeList) xpathFactory.newXPath().compile(xpath_url).evaluate(doc, XPathConstants.NODESET);
}

From source file:Main.java

public static XPath newXPath() {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    return xpath;
}

From source file:Main.java

/**
 * @param xpathExp/*from  ww w  .  j  av a2  s  .  c om*/
 * @return
 * @throws XPathExpressionException
 */
public static XPathExpression createXpath(String xpathExp) throws XPathExpressionException {
    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xPath = xFactory.newXPath();

    XPathExpression expr = xPath.compile(xpathExp);
    return expr;
}

From source file:Main.java

public static Vector<Node> findNodes(String xpath, Node scope) throws XPathExpressionException {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();
    NodeList nl;/*from  w  w w  . j  a  v a 2 s . com*/
    Vector<Node> elements = new Vector<Node>();

    nl = (NodeList) xp.evaluate(xpath, scope, XPathConstants.NODESET);

    for (int i = 0; i < nl.getLength(); i++)
        elements.add(nl.item(i));

    return elements;
}

From source file:Main.java

public static Vector<Element> findElements(String xpath, Node scope) throws XPathExpressionException {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();
    NodeList nl;/*from  w w w  .  j ava  2s  .  co m*/
    Vector<Element> elements = new Vector<Element>();

    nl = (NodeList) xp.evaluate(xpath, scope, XPathConstants.NODESET);

    for (int i = 0; i < nl.getLength(); i++)
        elements.add((Element) nl.item(i));

    return elements;
}

From source file:Main.java

public static Node xpathToNode(String xpathQuery, Object domObject) throws XPathExpressionException {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();

    return (Node) xpath.evaluate(xpathQuery, domObject, XPathConstants.NODE);
}

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);//  w w  w  .  j a  v a 2s  . c  om

    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

/**
 * //  w ww  .j a v  a2 s. co  m
 * @param xPath
 * @throws Exception - if path invalid.
 */
public static void isLegalXPath(final String xPath) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    xpath.compile(xPath);
}