Example usage for org.jdom2.xpath XPath addNamespace

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

Introduction

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

Prototype

public void addNamespace(String prefix, String uri) 

Source Link

Document

Adds a namespace definition (prefix and URI) to the list of namespaces known of this XPath expression.

Usage

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 a 2  s.  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);
}

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

License:Open Source License

public List<?> selectNodes(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);
    }// w  w w .  jav  a  2  s  .  co  m
    for (Namespace additionalNamespace : (List<Namespace>) scopeElement.getDocument().getRootElement()
            .getAdditionalNamespaces()) {
        xPath.addNamespace(additionalNamespace);
    }

    return xPath.selectNodes(scopeElement);
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public List<Attribute> getAttributes(Element scopeElement, String xPathExpression) throws JDOMException {
    List<Attribute> elems = new LinkedList<Attribute>();
    XPath xPath = XPath.newInstance(xPathExpression);
    String namespaceURI = scopeElement.getDocument().getRootElement().getNamespaceURI();
    if ((namespaceURI != null) && (!"".equals(namespaceURI))) {
        xPath.addNamespace("xp", namespaceURI);
    }/*www.  jav  a2  s.  c om*/
    for (Namespace additionalNamespace : (List<Namespace>) scopeElement.getDocument().getRootElement()
            .getAdditionalNamespaces()) {
        xPath.addNamespace(additionalNamespace);
    }
    for (Object obj : xPath.selectNodes(scopeElement)) {
        if (obj instanceof Attribute) {
            elems.add((Attribute) obj);
        }
    }

    return elems;
}

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

License:Open Source License

public List<Object> getNodes(Element scopeElement, String xPathExpression, String defaultPrefix)
        throws JDOMException {
    List<Object> nodes = new LinkedList<Object>();
    XPath xPath = XPath.newInstance(xPathExpression);
    String namespaceURI = scopeElement.getDocument().getRootElement().getNamespaceURI();
    if ((namespaceURI != null) && (!"".equals(namespaceURI))) {
        xPath.addNamespace(defaultPrefix, namespaceURI);
    }/*  w  ww.j a v a 2s  .  com*/
    for (Namespace additionalNamespace : (List<Namespace>) scopeElement.getDocument().getRootElement()
            .getAdditionalNamespaces()) {
        xPath.addNamespace(additionalNamespace);
    }
    for (Object obj : xPath.selectNodes(scopeElement)) {
        nodes.add(obj);
    }
    return nodes;
}