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 String getFirstValueFromXPath(Element parent, String xpath) throws Exception {
    try {//ww  w.j  a  v a2  s.  c  om
        XPath xPath = XPathFactory.newInstance().newXPath();
        return (String) xPath.compile(xpath).evaluate(parent, XPathConstants.STRING);
    } catch (XPathExpressionException xpee) {
        throw new Exception(xpee.getMessage());
    }
}

From source file:Main.java

/**
 * Get xpath expression object by xpath string
 * /*  w  w w .j a v  a  2s. c o  m*/
 * @param xpathStr
 * @return
 * @throws Exception
 */
public static XPathExpression getXPathExpression(String xpathStr) throws Exception {
    return XPathFactory.newInstance().newXPath().compile(xpathStr);
}

From source file:Main.java

public static Object evalXPathExpr(String pathExpr, Document xmlDocument, QName returnType) {
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xpathObj = xPathFactory.newXPath();
    XPathExpression expr;/* w w  w. j  a va  2 s . com*/
    Object result = null;
    try {
        expr = xpathObj.compile(pathExpr);
        result = expr.evaluate(xmlDocument, returnType);
    } catch (XPathExpressionException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static void deepCopyDocument(Document ttml, File target) throws IOException {
    try {//  w  ww .  j  av a  2s . co m
        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 Vector<Node> findNodes(String xpath, Node scope) throws XPathExpressionException {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();// w  ww.j a  v a  2 s  . c  o  m
    NodeList nl;
    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 synchronized boolean deleteNode(String charCode, Node root) {
    try {// w ww .  j  av a  2s . co  m
        XPath xpath = XPathFactory.newInstance().newXPath();
        Node node = (Node) xpath.evaluate(String.format(XPATH_EVAL_ID, charCode), root, XPathConstants.NODE);
        if (node != null) { // remove existing node
            Node parent = node.getParentNode();
            parent.removeChild(node);
            return true;
        }
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static String retrieveElementByXMLtag(String xPathExpression) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(xPathExpression);
    return expr.evaluate(doc, XPathConstants.STRING).toString();
}

From source file:Main.java

/**
 * @param xpathExp// ww w . j a v  a  2 s. c  o  m
 * @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 NodeList evalXpath(String xpath, Object item) throws XPathExpressionException {
    XPath xp = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xp.compile(xpath);
    return (NodeList) expr.evaluate(item, XPathConstants.NODESET);
}

From source file:Main.java

public static String xpath(String xml, String xpath) throws Exception {
    XPath xp = XPathFactory.newInstance().newXPath();
    Document doc = parse(xml);//from  w ww  . j  a  va2s .c  o m
    return xp.evaluate(xpath, doc.getDocumentElement());
}