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:com.semsaas.jsonxml.tools.JsonXpath.java

public static void main(String[] args) {
    /*//w w  w. jav a 2s  . c  om
     * Process options
     */
    LinkedList<String> files = new LinkedList<String>();
    LinkedList<String> expr = new LinkedList<String>();
    boolean help = false;
    String activeOption = null;
    String error = null;

    for (int i = 0; i < args.length && error == null && !help; i++) {
        if (activeOption != null) {
            if (activeOption.equals("-e")) {
                expr.push(args[i]);
            } else if (activeOption.equals("-h")) {
                help = true;
            } else {
                error = "Unknown option " + activeOption;
            }
            activeOption = null;
        } else {
            if (args[i].startsWith("-")) {
                activeOption = args[i];
            } else {
                files.push(args[i]);
            }
        }
    }

    if (error != null) {
        System.err.println(error);
        showHelp();
    } else if (help) {
        showHelp();
    } else {
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();

            for (String f : files) {
                System.out.println("*** " + f + " ***");
                try {
                    // Create a JSON XML reader
                    XMLReader reader = XMLReaderFactory.createXMLReader("com.semsaas.jsonxml.JsonXMLReader");

                    // Prepare a reader with the JSON file as input
                    InputStreamReader stringReader = new InputStreamReader(new FileInputStream(f));
                    SAXSource saxSource = new SAXSource(reader, new InputSource(stringReader));

                    // Prepare a DOMResult which will hold the DOM of the xjson
                    DOMResult domResult = new DOMResult();

                    // Run SAX processing through a transformer
                    // (This could be done more simply, but we have here the opportunity to pass our xjson through
                    // an XSLT and get a legacy XML output ;) )
                    transformer.transform(saxSource, domResult);
                    Node dom = domResult.getNode();

                    XPathFactory xpathFactory = XPathFactory.newInstance();
                    for (String x : expr) {
                        try {
                            XPath xpath = xpathFactory.newXPath();
                            xpath.setNamespaceContext(new NamespaceContext() {
                                public Iterator getPrefixes(String namespaceURI) {
                                    return null;
                                }

                                public String getPrefix(String namespaceURI) {
                                    return null;
                                }

                                public String getNamespaceURI(String prefix) {
                                    if (prefix == null) {
                                        return XJSON.XMLNS;
                                    } else if ("j".equals(prefix)) {
                                        return XJSON.XMLNS;
                                    } else {
                                        return null;
                                    }
                                }
                            });
                            NodeList nl = (NodeList) xpath.evaluate(x, dom, XPathConstants.NODESET);
                            System.out.println("-- Found " + nl.getLength() + " nodes for xpath '" + x
                                    + "' in file '" + f + "'");
                            for (int i = 0; i < nl.getLength(); i++) {
                                System.out.println(" +(" + i + ")+ ");
                                XMLJsonGenerator handler = new XMLJsonGenerator();
                                StringWriter buffer = new StringWriter();
                                handler.setOutputWriter(buffer);

                                SAXResult result = new SAXResult(handler);
                                transformer.transform(new DOMSource(nl.item(i)), result);

                                System.out.println(buffer.toString());
                            }
                        } catch (XPathExpressionException e) {
                            System.err.println("-- Error evaluating '" + x + "' on file '" + f + "'");
                            e.printStackTrace();
                        } catch (TransformerException e) {
                            System.err.println("-- Error evaluating '" + x + "' on file '" + f + "'");
                            e.printStackTrace();
                        }
                    }
                } catch (FileNotFoundException e) {
                    System.err.println("File '" + f + "' was not found");
                } catch (SAXException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TransformerException e) {
                    e.printStackTrace();
                }
            }
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static NodeList getNodeList(Node node, XPathExpression xpath) {
    try {/*from  w ww .  j  a  va 2 s .co m*/
        Object result = xpath.evaluate(node, XPathConstants.NODESET);
        return (NodeList) result;
    } catch (XPathExpressionException e) {
        throw new RuntimeException("Invalid XPath", e);
    }
}

From source file:Main.java

private static NodeList getNodeListByXpath(String xpath) throws Exception {
    XPath xPath = XPathFactory.newInstance().newXPath();
    String expression = xpath;// w  w w.  j  a v a  2s  .c o m

    NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
    return nodeList;
}

From source file:Main.java

public static NodeList getResultXpath(String expr, InputSource inputSource) throws XPathExpressionException {
    XPathExpression xExpr = xPath.compile(expr);
    NodeList evaluate = (NodeList) xExpr.evaluate(inputSource, XPathConstants.NODESET);
    return evaluate;
}

From source file:Main.java

public static NodeList findNodes(Document doc, String xpath) {
    return (NodeList) xpathFind(doc, xpath, XPathConstants.NODESET);
}

From source file:Main.java

public static NodeList select(Object item, String xpathExpression) throws Exception {
    XPath xpath = factory.newXPath();
    return (NodeList) xpath.evaluate(xpathExpression, item, XPathConstants.NODESET);
}

From source file:Main.java

public static NodeList nodesForXPath(Node node, String xPathString) throws XPathExpressionException {
    QName returnType = XPathConstants.NODESET;
    return (NodeList) getXPath(node, xPathString, returnType);
}

From source file:Main.java

public static NodeList getNodes(Node doc, String xpath_url) throws XPathExpressionException {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    return (NodeList) xpathFactory.newXPath().compile(xpath_url).evaluate(doc, XPathConstants.NODESET);
}

From source file:Main.java

public static NodeList readNodelistValFromXmlDocument(Document xmlDocument, String xpathQueryExpression,
        XPath xpathInstance) throws XPathExpressionException {

    return (NodeList) xpathInstance.compile(xpathQueryExpression).evaluate(xmlDocument, XPathConstants.NODESET);
}

From source file:Main.java

/**
 * @param doc/*from   ww  w .j  a  v  a 2 s.c om*/
 * @param expr
 * @return
 * @throws XPathExpressionException
 */
public static NodeList findNodes(Document doc, XPathExpression expr) throws XPathExpressionException {
    Object evaluate = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) evaluate;
    return nodes;
}