Example usage for javax.xml.xpath XPathConstants NODESET

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

Introduction

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

Prototype

QName NODESET

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

Click Source Link

Document

The XPath 1.0 NodeSet data type.

Maps to Java org.w3c.dom.NodeList .

Usage

From source file:Main.java

public static ArrayList<Element> getElementByXPath(Element el, String name) {
    try {//from w ww. ja v a  2 s. c  o m
        NodeList nl = (NodeList) XPathFactory.newInstance().newXPath().evaluate(name, el,
                XPathConstants.NODESET);
        return nodelistToElement(nl);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void deepCopyDocument(Document ttml, File target) throws IOException {
    try {//from  w w w  . j  a  v  a  2s . com
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile("//*/@backgroundImage");
        NodeList nl = (NodeList) expr.evaluate(ttml, XPathConstants.NODESET);
        for (int i = 0; i < nl.getLength(); i++) {
            Node backgroundImage = nl.item(i);
            URI backgroundImageUri = URI.create(backgroundImage.getNodeValue());
            if (!backgroundImageUri.isAbsolute()) {
                copyLarge(new URI(ttml.getDocumentURI()).resolve(backgroundImageUri).toURL().openStream(),
                        new File(target.toURI().resolve(backgroundImageUri).toURL().getFile()));
            }
        }
        copyLarge(new URI(ttml.getDocumentURI()).toURL().openStream(), target);

    } catch (XPathExpressionException e) {
        throw new IOException(e);
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static Element getElementByXpath(Element sourceRoot, String xpathExpress)
        throws XPathExpressionException {
    XPath xPath = XPATH_FACTORY.newXPath();
    NodeList nodes = (NodeList) xPath.evaluate(xpathExpress, sourceRoot, XPathConstants.NODESET);
    if (nodes.getLength() > 0)
        return (Element) nodes.item(0);
    return null;//from  w ww.  j a v a  2 s. c o  m
}

From source file:Main.java

public static Vector<Element> findElements(String xpath, Node scope) throws XPathExpressionException {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();/*w ww.  jav a  2  s .c o m*/
    NodeList nl;
    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

/**
 *  convenience method to evaluate an xpath expression against a DOM document
 * @param DOM the document//from www  .  j a  va  2 s  . c  om
 * @param xpath the xpath
 * @return NodeList of retrieved nodes
 * @throws XPathExpressionException
 */
public static NodeList evaluateXPath(Document DOM, XPathExpression xpath) throws XPathExpressionException {

    NodeList result = (NodeList) xpath.evaluate(DOM, XPathConstants.NODESET);

    return result;
}

From source file:Main.java

public static NodeList getNodes(Object doc, String query) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(query);
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    return nodes;
}

From source file:Main.java

public static NodeList selectNodeList(Node node, String str) throws XPathExpressionException {
    XPathExpression xPathExpression = null;
    NodeList nList = null;/*  w  w w .  j  a v  a2 s.c o m*/

    synchronized (getXPathBuilder()) {
        xPathExpression = getXPathBuilder().compile(str);

        nList = (NodeList) xPathExpression.evaluate(node, XPathConstants.NODESET);
    }

    return nList;
}

From source file:Main.java

public static NodeList getNodes(final Object doc, final String query) throws XPathExpressionException {
    final XPath xpath = XPathFactory.newInstance().newXPath();
    final XPathExpression expr = xpath.compile(query);
    return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
}

From source file:Main.java

public static List<String> getValueList(Node node, String exprStr) throws XPathExpressionException {
    ArrayList<String> r = new ArrayList<>();
    XPathExpression expr = xpath.compile(exprStr);
    NodeList nodes = (NodeList) expr.evaluate(node, XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); i++) {
        r.add(nodes.item(i).getNodeValue());
    }/*w ww.j  a v  a  2s . c o m*/
    return r;
}

From source file:Main.java

public static Iterable<Element> elementsByXpath(Element sourceRoot, String xpathExpress)
        throws XPathExpressionException {
    XPath xPath = XPATH_FACTORY.newXPath();
    NodeList nodes = (NodeList) xPath.evaluate(xpathExpress, sourceRoot, XPathConstants.NODESET);
    ArrayList<Element> elements = new ArrayList<Element>(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); ++i)
        elements.add((Element) nodes.item(i));
    return elements;
}