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 Node[] getMultiNode(InputStream is, String xpathStr)
        throws SAXException, IOException, XPathExpressionException {
    Node[] result = null;/*from   w  ww  .  ja va  2 s . c om*/
    Document doc = null;
    if (is == null)
        return result;
    doc = dombuilder.parse(is);
    Object obj = xpath.compile(xpathStr).evaluate(doc, XPathConstants.NODESET);
    if (obj != null) {
        NodeList nodes = (NodeList) obj;
        result = new Node[nodes.getLength()];
        for (int i = 0; i < nodes.getLength(); i++)
            result[i] = nodes.item(i);
    }
    return result;
}

From source file:Main.java

/**
 * Get xml nodes by xpath string, based on xml document
 * //from w  w  w.  j  a  v  a 2  s  .c o m
 * @param doc
 * @param xpath
 * @return
 * @throws Exception
 */
public static NodeList getNodesByXPath(Document doc, String xpath) throws Exception {
    return (NodeList) getXPathExpression(xpath).evaluate(doc, XPathConstants.NODESET);
}

From source file:Main.java

public static NodeList getNogeList(String path, Node node) throws RuntimeException {
    path = path.trim();// w w w  .  j av a2s .co  m
    try {
        XPath xp = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xp.compile(path);

        NodeList lst = (NodeList) expr.evaluate(node, XPathConstants.NODESET);
        return lst;

    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static String prettyXML(String xml, int indent) {
    try {//from w ww  . j  ava 2  s .  co m
        // Turn xml string into a document
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

        // Remove whitespaces outside tags
        XPath xPath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document,
                XPathConstants.NODESET);

        for (int i = 0; i < nodeList.getLength(); ++i) {
            Node node = nodeList.item(i);
            node.getParentNode().removeChild(node);
        }
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StringWriter stringWriter = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
        return stringWriter.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Use an Xpath expression on the given node or document.
 *
 * @param _xpathExpression xpath expression string
 * @param _xmlDocumentOrNode a {@link Document} or {@link Node} object
 * @return NodeList never null/*from w w w . j  a v  a2 s .c  o m*/
 * @throws IOException on error
 */
public static NodeList applyXpathExpressionToDocument(String _xpathExpression, Node _xmlDocumentOrNode)
        throws IOException {

    XPathFactory xfactory = XPathFactory.newInstance();
    XPath xpath = xfactory.newXPath();
    XPathExpression expr = null;
    try {
        expr = xpath.compile(_xpathExpression);
    } catch (XPathExpressionException _ex) {
        throw new IOException(_ex);
    }

    Object result = null;
    try {
        result = expr.evaluate(_xmlDocumentOrNode, XPathConstants.NODESET);
    } catch (Exception _ex) {
        throw new IOException(_ex);
    }

    return (NodeList) result;
}

From source file:Main.java

public static List<Node> selectMultipleNodes(String expression, Node node, NamespaceContext namespaceContext)
        throws XPathException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(namespaceContext);
    NodeList nodeList = (NodeList) xpath.evaluate(expression, node, XPathConstants.NODESET);
    List<Node> nodes = new ArrayList<Node>();
    int nodeCount = nodeList.getLength();
    for (int i = 0; i < nodeCount; i++) {
        nodes.add(nodeList.item(i));//from  w  w  w .  j  a va 2s.  co m
    }
    return nodes;
}

From source file:Main.java

public static Vector<HashMap> xmlToVector222(InputStream is, String xpath) {
    try {//from   w  w  w  . j a  v a2s. com
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        InputSource inputSource = new InputSource(is);

        NodeList nodes = (NodeList) xPath.evaluate(xpath, inputSource, XPathConstants.NODESET);
        Vector<HashMap> vector = new Vector<HashMap>();

        for (int x = 0; x < nodes.getLength(); x++) {
            NodeList nodeList = nodes.item(x).getChildNodes();
            HashMap hashmap = new HashMap();
            for (int y = 0; y < nodeList.getLength(); y++) {
                Node node = nodeList.item(y);
                if (!node.getNodeName().equals("#text")) {
                    hashmap.put(node.getNodeName(), node.getTextContent());
                }
            }
            vector.add(hashmap);
        }
        return vector;
    } catch (Exception ex) {
        ex.printStackTrace();
        return new Vector();
    }
}

From source file:Main.java

public static NodeList executeXPath(Document dom, String xpath) throws XPathExpressionException {
    return (NodeList) executeXPath(dom, xpath, XPathConstants.NODESET);
}

From source file:Main.java

public static NodeList selectNodes(String express, Object source) {
    NodeList result = null;/*from  w  w  w .  j a  va  2s .c o m*/
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        result = (NodeList) xpath.evaluate(express, source, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static NodeList getNodesByXPath(File file, String xpath) {
    if (!file.isFile() || !file.canRead())
        return null;

    Document document;//  w  ww.j a  v  a 2 s . c  om
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    try {
        DocumentBuilder documentBuilder = dbFactory.newDocumentBuilder();
        document = documentBuilder.parse(file);
    } catch (ParserConfigurationException e) {
        return null;
    } catch (SAXException e) {
        return null;
    } catch (IOException e) {
        return null;
    }

    if (document == null) {
        return null;
    }

    Object result;
    try {
        XPath xpathCompiler = XPathFactory.newInstance().newXPath();
        XPathExpression xPathExpr = xpathCompiler.compile(xpath);
        result = xPathExpr.evaluate(document, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        return null;
    }

    return (NodeList) result;
}