Example usage for javax.xml.xpath XPathFactory newXPath

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

Introduction

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

Prototype

public abstract XPath newXPath();

Source Link

Document

Return a new XPath using the underlying object model determined when the XPathFactory was instantiated.

Usage

From source file:Main.java

public static Vector<HashMap> xmlToVector222(InputStream is, String xpath) {
    try {/*from  ww  w  .  j a va2s .  c om*/
        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 XPath getSharedXPath() {
    XPath xPath = null;/*from  ww w  . ja va2 s  .  c om*/
    if (sharedXPath != null)
        xPath = sharedXPath.get();
    if (xPath == null) {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        sharedXPath = new SoftReference<XPath>(xPath = xPathFactory.newXPath());
    }
    return xPath;
}

From source file:Main.java

/**
 * /*  w w w . j a v a  2  s  .c  o  m*/
 * @param xPathS
 * @param node
 * @param nsuri
 * @param pre
 * @param returnType
 * @return Return type is one of XPathConstants .BOOLEAN, .NODE, .NODESET,
 *         .NUMBER, .STRING
 * @throws Exception
 */
public static Object getNodesListXpath(final String xPathS, final Node node, final String nsuri,
        final String pre, final QName returnType) throws Exception {
    Object matches = null;
    System.setProperty("javax.xml.xpath.XPathFactory:" + XPathConstants.DOM_OBJECT_MODEL, XPATH_FACTORY);

    XPathFactory xpathFactory = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL);
    XPath xpath = xpathFactory.newXPath();
    XPathExpression xpe = xpath.compile(xPathS);
    matches = xpe.evaluate(node, returnType);

    return matches;
}

From source file:Main.java

/**
 * Read application-context file, and return fully qualified class name for
 * given <code>beanName</code>
 * //  ww w.  j a  v  a  2s .co  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;
}

From source file:Main.java

public static String findInXml(String xml, String xpath) {
    try {//from  w  w w . j  a  v  a 2s .  com
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                //                    if (systemId.contains("foo.dtd")) {
                return new InputSource(new StringReader(""));
                //                    } else {
                //                        return null;
                //                    }
            }
        });
        Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPathExpression expr = xPathfactory.newXPath().compile(xpath);
        return (String) expr.evaluate(doc, XPathConstants.STRING);
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}

From source file:Main.java

public static String valueForXPath(String xpath, InputStream inputStream) throws XPathExpressionException {
    /*//  ww w  . j a va  2 s.  c o  m
     * Report.systemProperty("javax.xml.parsers.DocumentBuilderFactory");
     * Report.systemProperty("javax.xml.parsers.SAXParserFactory");
     * Report.systemProperty("javax.xml.transform.TransformerFactory");
     */
    XPathFactory factory = XPathFactory.newInstance();
    InputSource is = new InputSource(inputStream);
    XPath xp = factory.newXPath();
    return xp.evaluate(xpath, is);
}

From source file:Main.java

/**
 * @return//from w w w.ja va2s.  c o m
 */
public static XPath getCruxPagesXPath() {
    XPathFactory factory = XPathFactory.newInstance();
    XPath findPath = factory.newXPath();
    findPath.setNamespaceContext(new NamespaceContext() {
        public String getNamespaceURI(String prefix) {
            if (prefix.equals("c")) {
                return "http://www.cruxframework.org/crux";
            } else if (prefix.equals("v")) {
                return "http://www.cruxframework.org/view";
            }

            return "";
        }

        public String getPrefix(String namespaceURI) {
            if (namespaceURI.equals("http://www.cruxframework.org/crux")) {
                return "c";
            } else if (namespaceURI.equals("http://www.cruxframework.org/view")) {
                return "v";
            }
            return "";
        }

        public Iterator<?> getPrefixes(String namespaceURI) {
            List<String> prefixes = new ArrayList<String>();
            prefixes.add("c");
            prefixes.add("v");

            return prefixes.iterator();
        }
    });

    return findPath;
}

From source file:Main.java

public static XPath getXPathInstance() {
    XPathFactory factory = XPathFactory.newInstance();
    return factory.newXPath();
}

From source file:Main.java

public static NodeList getNodeList(Node node, String path) {
    try {/*from  ww  w . j a v a2 s. com*/
        slowGetValue++;
        // System.out.println("slow : " + slowGetValue + " - " + path);

        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        XPathExpression expr;
        expr = xpath.compile(path);
        NodeList list = (NodeList) expr.evaluate(node, XPathConstants.NODESET);

        return list;
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.thinkbiganalytics.feedmgr.nifi.NifiTemplateParser.java

/**
 * @param nifiTemplate the nifi template xml string
 * @return the name of the template//from   ww  w  . j a  va2 s.com
 */
public static String getTemplateName(String nifiTemplate)
        throws ParserConfigurationException, XPathExpressionException, IOException, SAXException {

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();

    InputSource source = new InputSource(new StringReader(nifiTemplate));
    String name = (String) xpath.evaluate("/template/name", source, XPathConstants.STRING);
    return name;
}