Example usage for org.jdom2.xpath XPathFactory compile

List of usage examples for org.jdom2.xpath XPathFactory compile

Introduction

In this page you can find the example usage for org.jdom2.xpath XPathFactory compile.

Prototype

public <T> XPathExpression<T> compile(String expression, Filter<T> filter) 

Source Link

Document

Create a XPathExpression<T> instance from this factory.

Usage

From source file:org.apache.wiki.util.XmlUtil.java

License:Apache License

/**
 * Parses the given XML file and returns the requested nodes. If there's an error accessing or parsing the file, an
 * empty list is returned.//from   w w  w .  ja  v a  2  s .  co  m
 * 
 * @param xml file to parse; matches all resources from classpath, filters repeated items.
 * @param requestedNodes requestd nodes on the xml file
 * @return the requested nodes of the XML file.
 */
public static List<Element> parse(String xml, String requestedNodes) {
    if (StringUtils.isNotEmpty(xml) && StringUtils.isNotEmpty(requestedNodes)) {
        Set<Element> readed = new HashSet<Element>();
        SAXBuilder builder = new SAXBuilder();
        try {
            Enumeration<URL> resources = XmlUtil.class.getClassLoader().getResources(xml);
            while (resources.hasMoreElements()) {
                URL resource = resources.nextElement();
                log.debug("reading " + resource.toString());
                Document doc = builder.build(resource);
                XPathFactory xpfac = XPathFactory.instance();
                XPathExpression<Element> xp = xpfac.compile(requestedNodes, Filters.element());
                readed.addAll(xp.evaluate(doc)); // filter out repeated items
            }
            return new ArrayList<Element>(readed);
        } catch (IOException ioe) {
            log.error("Couldn't load all " + xml + " resources", ioe);
        } catch (JDOMException jdome) {
            log.error("error parsing " + xml + " resources", jdome);
        }
    }
    return Collections.<Element>emptyList();
}

From source file:org.apache.wiki.util.XmlUtil.java

License:Apache License

/**
 * Parses the given stream and returns the requested nodes. If there's an error accessing or parsing the stream, an
 * empty list is returned.//from  ww w  . j  a  v a  2  s .  c o  m
 * 
 * @param xmlStream stream to parse.
 * @param requestedNodes requestd nodes on the xml stream.
 * @return the requested nodes of the XML stream.
 */
public static List<Element> parse(InputStream xmlStream, String requestedNodes) {
    if (xmlStream != null && StringUtils.isNotEmpty(requestedNodes)) {
        SAXBuilder builder = new SAXBuilder();
        try {
            Document doc = builder.build(xmlStream);
            XPathFactory xpfac = XPathFactory.instance();
            XPathExpression<Element> xp = xpfac.compile(requestedNodes, Filters.element());
            return xp.evaluate(doc);
        } catch (IOException ioe) {
            log.error("Couldn't load all " + xmlStream + " resources", ioe);
        } catch (JDOMException jdome) {
            log.error("error parsing " + xmlStream + " resources", jdome);
        }
    }
    return Collections.<Element>emptyList();
}