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 NodeList getNodes(String expression, Node root) {
    XPath xpath = xPathFactory.newXPath();
    try {//from   w  ww  .j a va2s  . com
        NodeList list = (NodeList) xpath.evaluate(expression, root, XPathConstants.NODESET);
        return list;
    } catch (XPathExpressionException xpe) {
        throw new IllegalStateException(xpe);
    }
}

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 av  a  2 s  .c o  m
    NodeList n1 = null;
    expr = xpath.compile("//*[@" + attribute + "]");
    n1 = (NodeList) expr.evaluate(xmlDocument, XPathConstants.NODESET);
    return n1;

}

From source file:Main.java

public static ArrayList<Node> getNodesXPath(String srcXmlString, String xPath) {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(false); // never forget this!
    Document doc = null;//from  w ww.j  a  va  2  s  .c  o  m
    DocumentBuilder builder = null;
    ArrayList<Node> nodesList = new ArrayList<Node>();
    try {
        builder = domFactory.newDocumentBuilder();
        doc = builder.parse(new ByteArrayInputStream(srcXmlString.getBytes()));
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(xPath);

        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        NodeList xPathNodes = (NodeList) result;
        logger.debug("xpath result count: " + xPathNodes.getLength());
        // iterate through all the nodes
        for (int i = 0; i < xPathNodes.getLength(); i++) {
            nodesList.add(xPathNodes.item(i));
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage());
    }
    return nodesList;
}

From source file:Main.java

/**
 * Select node list what matches given xpath query
 *
 * @param node       xml node/*from  w  w  w  . j  av a 2  s . co m*/
 * @param expression xpath query
 * @return nodes which confirms given xpath query.
 * @throws XPathExpressionException in case of any errors.
 */
public static NodeList query(final Node node, final String expression) throws XPathExpressionException {
    final XPath xpath = XPathFactory.newInstance().newXPath();
    return (NodeList) xpath.evaluate(expression, node, XPathConstants.NODESET);
}

From source file:Main.java

public static Map<String, String> readXmlToMap(String filename, String xpathExp, String arrtibute)
        throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
    //to-do something
    Map<String, String> dataMap = new HashMap<String, String>();

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);

    DocumentBuilder db = dbf.newDocumentBuilder();

    File file = new File(filename);
    if (file.exists()) {
        Document doc = db.parse(file);
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();

        NodeList varList = (NodeList) xpath.evaluate(xpathExp, doc, XPathConstants.NODESET);

        for (int i = 0; i < varList.getLength(); i++) {
            dataMap.put(varList.item(i).getAttributes().getNamedItem(arrtibute).getNodeValue(),
                    varList.item(i).getTextContent());
        }//from   ww  w .  j av  a  2  s .c  om
    }
    return dataMap;
}

From source file:Main.java

/**
 * Return all the nodes in an Document for a given XPath.
 *
 * @param doc        the document to read from
 * @param pathToNode the XPath to find/*  www .ja  va 2 s.  c  om*/
 * @return a NodeList object
 * @throws XPathExpressionException if  xpath exception occurred
 */
public static NodeList readNode(Document doc, String pathToNode, NamespaceContext ctx)
        throws XPathExpressionException {
    XPath xPath = createXpath();
    xPath.setNamespaceContext(ctx);
    return (NodeList) xPath.evaluate(pathToNode, doc, XPathConstants.NODESET);
}

From source file:Main.java

/**
 * Returns the XML {@link NodeList} matching the given XPath expression.
 * @param expression XPath expression.//from w w  w .  j av  a  2s .  co  m
 * @param document XML document to search for the node list.
 * @return The matching XML {@link NodeList}.
 */
public static NodeList evaluateXPathAsNodeList(String expression, Document document) {
    return evaluateXpath(expression, document, XPathConstants.NODESET);
}

From source file:Main.java

public static NodeList getNodeListFromElement(Document document, String expression)
        throws XPathExpressionException {
    XPath xPath = getxPathFactory().newXPath();
    XPathExpression xPathExpression = xPath.compile(expression);
    NodeList nodeList = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);
    return nodeList;
}

From source file:Main.java

static void getHTTPXml(URL url) throws Exception {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("ACCEPT", "application/xml");
    InputStream xml = conn.getInputStream();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    org.w3c.dom.Document document = builder.parse(xml);

    System.out.println(document);
    String doctype = conn.getContentType();
    System.out.println(doctype);/*from ww w.ja v  a  2  s .c  o  m*/

    XPathFactory pathFactory = XPathFactory.newInstance();
    XPath path = pathFactory.newXPath();
    XPathExpression expression;
    expression = path.compile("/result/checkid");
    NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
    String checkids[] = getNodeValue(nodeList);
    for (String checkid : checkids) {
        System.out.print(checkid + ", ");
    }
    conn.disconnect();
}

From source file:Main.java

/**
 * Read application-context file, and return fully qualified class name for
 * given <code>beanName</code>
 * //from w w  w . jav  a2  s .  c o m
 * @param beanName
 * @return
 * 
 */
public static String getFullyQualifiedClass(String beanName) {

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

    docBuilderFactory.setNamespaceAware(true);

    String nodeValue = "";

    try {
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        Document doc = builder.parse("application-context.xml");

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("//bean[@name='" + beanName + "']/@class");

        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        NodeList nodes = (NodeList) result;
        if (nodes.getLength() > 0) {
            nodeValue = nodes.item(0).getNodeValue();
        }
    } catch (ParserConfigurationException parserConfigurationException) {
        parserConfigurationException.printStackTrace();
    } catch (IOException ioException) {
        ioException.printStackTrace();
    } catch (SAXException saxException) {
        saxException.printStackTrace();
    } catch (XPathExpressionException xPathExpressionException) {
        xPathExpressionException.printStackTrace();
    }

    return nodeValue;
}