Example usage for org.jdom2.xpath XPath selectSingleNode

List of usage examples for org.jdom2.xpath XPath selectSingleNode

Introduction

In this page you can find the example usage for org.jdom2.xpath XPath selectSingleNode.

Prototype

abstract public Object selectSingleNode(Object context) throws JDOMException;

Source Link

Document

Evaluates the wrapped XPath expression and returns the first entry in the list of selected nodes (or atomics).

Usage

From source file:com.ardor3d.extension.model.collada.jdom.ColladaDOMUtil.java

License:Open Source License

/**
 * Select nodes through an XPath query and returns the first hit
 * /*from  w  w  w  .ja v a  2s .  c  o  m*/
 * @param element
 *            root element to start search on
 * @param query
 *            XPath expression
 * @return the first selected item, which may be of types: {@link Element}, {@link Attribute}, {@link Text},
 *         {@link CDATA}, {@link Comment}, {@link ProcessingInstruction}, Boolean, Double, String, or
 *         <code>null</code> if no item was selected.
 */
public Object selectSingleNode(final Element element, final String query) {
    final XPath xPathExpression = getXPathExpression(query);

    try {
        return xPathExpression.selectSingleNode(element);
    } catch (final JDOMException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.jahia.utils.osgi.parsers.AbstractXmlFileParser.java

License:Open Source License

/**
 * Utility method to retrieve an XML element using an XPath expression. Note that this method is
 * namespace aware and will require you to use the "xp" prefix in your XPath queries. For example, an XPath query
 * for a Spring XML configuration will look like this :
 * /xp:beans/xp:bean[@id="FileListSync"]/xp:property[@name="syncUrl"]
 * Currently there is no way to rename the prefix.
 *
 * @param scopeElement    the scope in which to execute the XPath query
 * @param xPathExpression the XPath query to select the element we wish to retrieve. In the case where multiple
 *                        elements match, only the first one will be returned.
 * @return the first element that matches the XPath expression, or null if no element matches.
 * @throws JDOMException raised if there was a problem navigating the JDOM structure.
 *//*ww  w.  j  a  v a2s  .c  o m*/
public static Element getElement(Element scopeElement, String xPathExpression) throws JDOMException {
    XPath xPath = XPath.newInstance(xPathExpression);
    String namespaceURI = scopeElement.getDocument().getRootElement().getNamespaceURI();
    if ((namespaceURI != null) && (!"".equals(namespaceURI))) {
        xPath.addNamespace("xp", namespaceURI);
    }
    for (Namespace additionalNamespace : (List<Namespace>) scopeElement.getDocument().getRootElement()
            .getAdditionalNamespaces()) {
        xPath.addNamespace(additionalNamespace);
    }
    return (Element) xPath.selectSingleNode(scopeElement);
}