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 Object evaluateXPath(String xpath, Object item, QName returnType) {
    try {// w ww.  j  av  a2s  .  c o  m
        return XPathFactory.newInstance().newXPath().compile(xpath).evaluate(item, returnType);
    } catch (XPathExpressionException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:Main.java

/**
 * Return a list of elements based on xPath
 *//*from  ww  w  .  j a va2 s.c  o  m*/
static NodeList getElementsByAttribute(Document doc, String xPath) {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = null;
    NodeList nl = null;
    try {
        expr = xpath.compile(xPath);
        nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return nl;
}

From source file:Main.java

public static NodeList SelectNodes(Document doc, String xmlpath) {
    NodeList nodes = null;// w w  w  .  j a v a  2 s  . com
    Object result;
    try {
        XPathFactory xFactory = XPathFactory.newInstance();
        XPath xpath = xFactory.newXPath();

        XPathExpression expr;
        expr = xpath.compile(xmlpath);
        result = expr.evaluate(doc, XPathConstants.NODESET);
        nodes = (NodeList) result;
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return nodes;
}

From source file:Main.java

public static List<Element> getXPathList(Node node, String expr) {
    assertNotNull("node == null", node);
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    List<Element> result = new ArrayList<Element>();
    try {/* ww w .  j  ava2 s . c  o m*/
        NodeList nl = (NodeList) xpath.evaluate(expr, node, XPathConstants.NODESET);
        for (int i = 0; i < nl.getLength(); i++) {
            result.add((Element) nl.item(i));
        }
        return result;
    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Return one element based on xPath/*from  ww  w .  jav  a2  s.co  m*/
 */
static Element getElementByAttribute(Document doc, String xPath) {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = null;
    NodeList nl = null;
    try {
        expr = xpath.compile(xPath);
        nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return (Element) nl.item(0);
}

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 XPath getSchemaXPath() {
    XPath xPath = schemaXPathThreadLocal.get();

    if (xPath == null) {
        xPath = XPathFactory.newInstance().newXPath();

        xPath.setNamespaceContext(new NamespaceContext() {
            @Override/*  w w  w .  j av  a2  s  .c  om*/
            public String getNamespaceURI(String prefix) {
                return XMLConstants.W3C_XML_SCHEMA_NS_URI;
            }

            @Override
            public String getPrefix(String namespaceURI) {
                return "xs";
            }

            @Override
            public Iterator getPrefixes(String namespaceURI) {
                return Arrays.asList("xs").iterator();
            }
        });
        schemaXPathThreadLocal.set(xPath);
    }

    return xPath;
}

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

/**
 * convenience method to generate an XPathExpression object from a string representing an Xpath
 * @param pathstring the string//ww  w  . j av a  2  s .  co m
 * @return an Xpath object, or null if there was an exception
 */
public static XPathExpression makeXPathFromString(String pathstring) {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = null;
    try {
        expr = xpath.compile(pathstring);
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }
    return expr;
}

From source file:Main.java

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