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

/**
 * convenience method to generate an XPathExpression object from a string representing an Xpath
 * @param pathstring the string// w w  w .j  av  a  2 s. c  o  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 Object getByXPath(Node parentNode, NamespaceContext context, String xPathExpression, QName type)
        throws XPathExpressionException {
    XPathFactory xPathFactory = xPathFactoryCache.get();
    XPath xPath = xPathFactory.newXPath();
    xPath.setNamespaceContext(context);/*from  w  ww .j av  a 2 s  .c o  m*/
    return xPath.evaluate(xPathExpression, parentNode, type);
}

From source file:Main.java

private static Object getValue(final String xpathExpression, final Node node, final QName type,
        final NamespaceContext nsContext) throws XPathExpressionException {

    final XPathFactory factory = XPathFactory.newInstance();
    final XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(nsContext);
    return xpath.evaluate(xpathExpression, node, type);
}

From source file:Main.java

public static NodeList executeXpathQuery(Node root, String query) throws TransformerException {

    try {/*from w  ww .  ja v a2 s . c  om*/
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(query);

        Object result = expr.evaluate(root, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        return nodes;
    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static List<Node> xpathToNodeList(String xpathQuery, Object domObject) throws XPathExpressionException {
    List<Node> result = new ArrayList<Node>();

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    NodeList nodeList = (NodeList) xpath.evaluate(xpathQuery, domObject, XPathConstants.NODESET);

    for (int i = 0; i < nodeList.getLength(); i++)
        result.add(nodeList.item(i));//from  w w  w.  j  a  v a2  s  .  co  m

    return result;
}

From source file:Main.java

public static NodeList getComponentsWithAttribute(Node xmlDocument, String attribute)
        throws XPathExpressionException {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = null;//w w w.  j  a v  a 2  s .  co  m
    NodeList n1 = null;
    expr = xpath.compile("//*[@" + attribute + "]");
    n1 = (NodeList) expr.evaluate(xmlDocument, XPathConstants.NODESET);
    return n1;

}

From source file:Main.java

public static Object executeXPath(Document document, String expression, QName outputType) {
    Object result = null;/*  w ww.  j  a  v  a2 s  .  c o m*/
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr;
    try {
        expr = xpath.compile(expression);
        result = expr.evaluate(document, outputType);
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result;
}

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

/**
 * Return a list of elements based on xPath
 *//*from  w w w  .j a  v a  2  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

/**
 * Return one element based on xPath//  w w  w. j  a  v a  2s. c o 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);
}