Example usage for javax.xml.xpath XPathFactory newInstance

List of usage examples for javax.xml.xpath XPathFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.xpath XPathFactory newInstance.

Prototype

public static XPathFactory newInstance() 

Source Link

Document

Get a new XPathFactory instance using the default object model, #DEFAULT_OBJECT_MODEL_URI , the W3C DOM.

This method is functionally equivalent to:

 newInstance(DEFAULT_OBJECT_MODEL_URI) 

Since the implementation for the W3C DOM is always available, this method will never fail.

Usage

From source file:com.semsaas.jsonxml.tools.JsonXpath.java

public static void main(String[] args) {
    /*/*  w ww. j  av  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:XPathResolver.java

public static void main(String[] args) {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    // Set the NamespaceContext
    xpath.setNamespaceContext(new MyNamespaceContext());
    // Set the function resolver
    xpath.setXPathFunctionResolver(new MyFunctionResolver());
    // Set the variable resolver
    xpath.setXPathVariableResolver(new MyVariableResolver());

    Object result = null;/*w  ww .  ja v a2s  .  c  om*/
    try {
        result = xpath.evaluate(EXPR, (Object) null, XPathConstants.NUMBER);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // The evaluation result is 9.0.
    System.out.println("The evaluation result: " + result);
}

From source file:Main.java

private static XPath getXPath() {
    return XPathFactory.newInstance().newXPath();
}

From source file:Main.java

public static XPath instantiateXPath() {

    return XPathFactory.newInstance().newXPath();
}

From source file:Main.java

public static XPath getXPathBuilder() {
    XPathFactory xpathfactory = XPathFactory.newInstance();
    XPath xpath = xpathfactory.newXPath();
    return xpath;
}

From source file:Main.java

private static XPath getXPath() {
    if (xPath == null)
        xPath = XPathFactory.newInstance().newXPath();

    return xPath;
}

From source file:Main.java

private static NodeList getNodeListByXpath(String xpath) throws Exception {
    XPath xPath = XPathFactory.newInstance().newXPath();
    String expression = xpath;/*from   ww  w. ja va 2s  .  co  m*/

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

From source file:Main.java

public static XPathFactory getXPathFactory() {
    if (xPathFactory == null)
        xPathFactory = XPathFactory.newInstance();
    return xPathFactory;
}

From source file:Main.java

/**
 * Creates new {@link XPath}.//from  w  w w  .  ja  v a 2 s  . c  o m
 * 
 * @return new {@link XPath}.
 */
public static XPath newXPath() {
    return XPathFactory.newInstance().newXPath();
}

From source file:Main.java

private static XPathExpression getXPath(String xpath) throws XPathExpressionException {
    return XPathFactory.newInstance().newXPath().compile(xpath);
}