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

/**
 * Evaluates a XPath expression from a XML node, returning a NodeList.
 * /*from  w  w  w  .  j  a  v a 2s  .  c om*/
 * @param expression
 *            A XPath expression
 * @param node
 *            The NodeList for which this expression should be evaluated
 * 
 * @return The result of evaluating the XPath expression to the given or
 *         <code>null</code> if an ecxception occured NodeList
 */
public static NodeList evaluateXPathExpressionAndReturnNodeList(String expression, Node node) {
    try {
        return (NodeList) getXPath().get().evaluate(expression, node, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        return null;
    }
}

From source file:Main.java

public static NodeList getNodes(String expression, Document document) {
    XPath xpath = xPathFactory.newXPath();
    try {/*from   w  w  w  . j  a  v  a 2 s.c  o  m*/
        NodeList list = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);
        return list;
    } catch (XPathExpressionException xpe) {
        throw new IllegalStateException(xpe);
    }
}

From source file:Main.java

/**
 * Returns the xml value.//from  www  . j  a  v  a  2s .c o  m
 * 
 * @param sIn
 * @param sxpath
 * @return
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 * @throws XPathExpressionException
 */
public static String XPathValueFromString(String sIn, String sxpath)
        throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
    // DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    Document doc = loadXMLFromString(sIn);
    XPath xPath = XPathFactory.newInstance().newXPath();
    // XPath Query for showing all nodes value
    XPathExpression expr = xPath.compile(sxpath);

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    String sReturn = "";
    for (int i = 0; i < nodes.getLength(); i++) {
        sReturn = nodes.item(i).getNodeValue();
    }
    return sReturn;
}

From source file:Main.java

/**
 * Evaluate XPath expression and return list of nodes.
 *
 * @param expression//from  w  w  w  . j  a va2s . co m
 *            XPath expression
 * @param node
 *            DOM node
 * @return list of DOM nodes
 * @throws XPathExpressionException
 *             if expression cannot be evaluated
 */
public static NodeList evaluateXPathExpression(final String expression, final Node node)
        throws XPathExpressionException {
    return (NodeList) evaluateXPathExpression(expression, node, XPathConstants.NODESET);
}

From source file:Main.java

private static List<String> getFemaleEmployeesName(Document doc, XPath xpath) throws Exception {
    List<String> list = new ArrayList<>();
    XPathExpression expr = xpath.compile("/Employees/Employee[gender='Female']/name/text()");
    // evaluate expression result on XML document
    NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); i++)
        list.add(nodes.item(i).getNodeValue());
    return list;/*from ww w .  j av a 2s  .  co  m*/
}

From source file:Main.java

/**
 * Create a map based on a xml file and a xpath expression and an attribute.
 * Treat attribute's value as map's key, treat xpath expression represented node's text content as map's value.
 * /*from  w  ww .  jav a  2s . c  o m*/
 * @param xmlFile handled xml file
 * @param xpathExp xpath express, such as "//var"
 * @param arrtibute attribute, such as "name"
 * @return created Map object
 * @throws Exception
 */
public static Map<String, String> readXmlToMap(String xmlFile, String xpathExp, String arrtibute)
        throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder db = factory.newDocumentBuilder();
    Document xmlDoc = null;
    try {
        xmlDoc = db.parse(new File(xmlFile));
    } catch (SAXException e) {
        if ("Premature end of file.".equals(e.getMessage()))
            System.out.println("Maybe your local data file has no content.");
        //e.printStackTrace();
        return new HashMap<String, String>();
    }
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    NodeList varList = (NodeList) xpath.evaluate(xpathExp, xmlDoc, XPathConstants.NODESET);
    Map<String, String> dataMap = new HashMap<String, String>();
    for (int i = 0; i < varList.getLength(); i++)
        dataMap.put(varList.item(i).getAttributes().getNamedItem(arrtibute).getNodeValue(),
                varList.item(i).getTextContent());

    return dataMap;
}

From source file:Main.java

/**
 * Checks in under a given root element whether it can find a child elements
 * which match the XPath expression supplied. Returns a {@link List} of
 * {@link Element} if they exist.// w w  w.  j a  v a2 s.  c  o  m
 * 
 * Please note that the XPath parser used is NOT namespace aware. So if you
 * want to find a element <beans><sec:http> you need to use the following
 * XPath expression '/beans/http'.
 * 
 * @param xPathExpression the xPathExpression
 * @param root the parent DOM element
 * @return a {@link List} of type {@link Element} if discovered, otherwise an empty list (never null)
 */
public static List<Element> findElements(String xPathExpression, Element root) {
    List<Element> elements = new ArrayList<Element>();
    NodeList nodes = null;

    try {
        XPathExpression expr = compiledExpressionCache.get(xPathExpression);
        if (expr == null) {
            expr = xpath.compile(xPathExpression);
            compiledExpressionCache.put(xPathExpression, expr);
        }
        nodes = (NodeList) expr.evaluate(root, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        throw new IllegalArgumentException("Unable evaluate xpath expression", e);
    }

    for (int i = 0, n = nodes.getLength(); i < n; i++) {
        elements.add((Element) nodes.item(i));
    }
    return elements;
}

From source file:Main.java

/**
 * Checks in under a given root element whether it can find a child elements
 * which match the XPath expression supplied. Returns a {@link List} of
 * {@link Element} if they exist. Please note that the XPath parser used is
 * NOT namespace aware. So if you want to find a element <beans><sec:http>
 * you need to use the following XPath expression '/beans/http'.
 * //  w  ww  . j  a  v  a 2 s  .  co  m
 * @param xPathExpression the xPathExpression
 * @param root the parent DOM element
 * @return a {@link List} of type {@link Element} if discovered, otherwise
 *         an empty list (never null)
 */
public static List<Element> findElements(final String xPathExpression, final Element root) {
    final List<Element> elements = new ArrayList<Element>();
    NodeList nodes = null;

    try {
        XPathExpression expr = COMPILED_EXPRESSION_CACHE.get(xPathExpression);
        if (expr == null) {
            expr = XPATH.compile(xPathExpression);
            COMPILED_EXPRESSION_CACHE.put(xPathExpression, expr);
        }
        nodes = (NodeList) expr.evaluate(root, XPathConstants.NODESET);
    } catch (final XPathExpressionException e) {
        throw new IllegalArgumentException("Unable evaluate xpath expression", e);
    }

    for (int i = 0, n = nodes.getLength(); i < n; i++) {
        elements.add((Element) nodes.item(i));
    }
    return elements;
}

From source file:Main.java

/**
 * Evaluates given XPath expression and returns node list. 
 * @param expr XPath expression/* w ww . ja va  2 s . c  om*/
 * @param fileName path of the XML file to be parsed
 * @return 
 * @throws XPathExpressionException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static NodeList getNodeList(final String expr, final File xmlFile)
        throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {

    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xPath.evaluate(expr, parseXMLFile(xmlFile), XPathConstants.NODESET);

    return nodeList;
}

From source file:Main.java

/**
 * Get the list of nodes from the specified object using the specified XPath expression.
 *
 * @param obj the object//from   w  w w  . j av  a2  s .  com
 * @param xpathExpression the XPath expression
 * @return the list of nodes from the specified object matched using the specified XPath expression.
 */
public static List<Node> getByXPath(Object obj, String xpathExpression) {
    NodeList list1 = evaluate(obj, xpathExpression, XPathConstants.NODESET);

    List<Node> nodes = new ArrayList<Node>();
    for (int i = 0; i < list1.getLength(); i++) {
        nodes.add(list1.item(i));
    }

    return nodes;
}