Example usage for org.w3c.dom Element getElementsByTagNameNS

List of usage examples for org.w3c.dom Element getElementsByTagNameNS

Introduction

In this page you can find the example usage for org.w3c.dom Element getElementsByTagNameNS.

Prototype

public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException;

Source Link

Document

Returns a NodeList of all the descendant Elements with a given local name and namespace URI in document order.

Usage

From source file:Main.java

/**
 * An Iterable for the Element's childs, with a particular name, of a node
 * //from   ww  w.ja  v a 2 s.  c o m
 * @param n
 *            the node get the children from
 * @param namespace
 * @param elementName
 *            the name of the child elements
 * @return An Iterable for the Element's children, with a particular name, of a node
 */
public static Iterable<Element> elements(Element n, String namespace, String elementName) {
    NodeList subNodes = n.getElementsByTagNameNS(namespace, elementName);
    int sz = subNodes.getLength();
    ArrayList<Element> elements = new ArrayList<Element>(sz);
    for (int idx = 0; idx < sz; idx++) {
        Node node = subNodes.item(idx);
        elements.add((Element) node);
    }
    return elements;
}

From source file:Main.java

/**
 * Get text content from element by namespace.
 *
 * @param element      element/*from  ww w.  jav  a 2  s. c  o m*/
 * @param namespaceURI Namespace URI
 * @param localName    local name
 *
 * @return Text content.
 */
public static String getContentFromElement(Element element, String namespaceURI, String localName) {
    String elementContent = null;
    NodeList nodes = element.getElementsByTagNameNS(namespaceURI, localName);
    for (int i = 0; i < nodes.getLength(); i++) {
        elementContent = nodes.item(i).getTextContent();
    }
    return elementContent;
}

From source file:Main.java

public static Element findElement(Element parent, String elementNS, String elementName, String attrName,
        String attrValue) {//ww  w . ja  v  a2 s .c o  m
    NodeList l = parent.getElementsByTagNameNS(elementNS, elementName);

    for (int i = 0; i < l.getLength(); i++) {
        Element e = (Element) l.item(i);
        String val = e.getAttribute(attrName);

        if (val != null && val.equals(attrValue)) {
            return e;
        }
    }

    return null;
}

From source file:Main.java

/**
 * Extracts the element named <var>name</var> with the namespace
 * <var>elementNS</var> that is a descentant of the element
 * <var>message</var>. This method returns null if the named elemens is not
 * a child of <var>message</var> or if <var>message</var> contains more than
 * one element named <var>name</var>
 * //from w w w.j a v  a2  s  . co  m
 * @param message
 *            Source Document that contains the element named
 *            <var>name</var>
 * @param elementNS
 *            Namespace of the element named <var>name</var>
 * @param name
 *            Name of the element to be extracted
 * @return Element named <var>name</var>, <br>
 *         or null if <var>message</var> contains zero or more than one
 *         element named <var>name</var>
 */
public static Element extractElement(Element message, String elementNS, String name, boolean strict) {
    NodeList list = message.getElementsByTagNameNS(elementNS, name);
    if (list == null || list.getLength() == 0 || (strict && list.getLength() > 1))
        return null;
    Element element = (Element) list.item(0);
    return element;
}

From source file:org.kitodo.sruimport.ResponseHandler.java

private static Element getXmlElement(Element parentNode, String elementTag) {
    NodeList nodeList = parentNode.getElementsByTagNameNS(ResponseHandler.MODS_NAMESPACE, elementTag);
    return (Element) nodeList.item(0);
}

From source file:Main.java

public final static Element firstChild(Element root, String ns, String path) {
    String names[] = path.split("/");
    for (int i = 0; i < names.length; i++) {
        String name = names[i];//from   ww  w  .  j  a  v  a  2 s  .  c o  m
        NodeList nl = root.getElementsByTagNameNS(ns, name);
        if (nl == null) {
            return null;
        }
        int len = nl.getLength();
        if (len < 1) {
            return null;
        }
        root = (Element) (nl.item(0));
    }
    return root;
}

From source file:XMLUtils.java

/**
 * Returns a list of value for the given node.
 * @param ns the namespace./*from ww w. jav  a  2 s  .  c  o  m*/
 * @param base the element from where to search.
 * @param name of the element to get.
 * @return the list of value of this element.
 */
public static List<String> getStringListValueElement(final String ns, final Element base, final String name) {
    List<String> returnedlist = new ArrayList<String>();

    // Get element
    NodeList list = base.getElementsByTagNameNS(ns, name);
    int length = list.getLength();

    // Get all values of all elements
    if (length > 0) {
        for (int i = 0; i < length; i++) {
            Element element = (Element) list.item(i);
            Node node = element.getFirstChild();
            if (node != null) {
                returnedlist.add(node.getNodeValue());
            }
        }
    }
    return returnedlist;
}

From source file:Main.java

public static List<Element> getElementsByTagNameNS(Element parent, String uri, String name, boolean localOnly) {
    List<Element> ret = new ArrayList<Element>();
    if (!localOnly) {
        NodeList elementList = parent.getElementsByTagNameNS(uri, name);
        for (int i = 0; i < elementList.getLength(); i++) {
            ret.add((Element) elementList.item(i));
        }//from  www. j  a  v a 2s . c  o  m
    } else {
        NodeList childList = parent.getChildNodes();
        for (int i = 0; i < childList.getLength(); i++) {
            if (childList.item(i).getNodeType() != Node.ELEMENT_NODE)
                continue;
            Element child = (Element) childList.item(i);
            if ((uri.equals("*") || child.getNamespaceURI().equals(uri))
                    && ((child.getLocalName() == null && uri.equals("*") && child.getTagName().equals(name))
                            || child.getLocalName().equals(name)))
                ret.add(child);
        }
    }
    return ret;
}

From source file:Main.java

public static Element getElementByTagName(Element current, String name, String namespace) {

    NodeList elements = null;/*from   ww  w .ja  v a 2s.c o m*/

    if (namespace == null)
        elements = current.getElementsByTagName(name);
    else
        elements = current.getElementsByTagNameNS(namespace, name);

    if (elements == null)
        return null;

    if (elements.getLength() != 1)
        return null;

    return (Element) elements.item(0);

}

From source file:XMLUtils.java

/**
 * Returns a Properties object matching the given node.
 * @param ns the namespace.//ww  w  .  j av a2  s  . c  om
 * @param base the element from where to search.
 * @param name of the element to get.
 * @return the value of this element.
 */
public static Properties getPropertiesValueElement(final String ns, final Element base, final String name) {
    Properties returnedProperties = new Properties();

    // Get element
    NodeList list = base.getElementsByTagNameNS(ns, name);
    if (list.getLength() == 1) {
        Element element = (Element) list.item(0);

        // Get property element
        NodeList properties = element.getElementsByTagNameNS(ns, "property");

        // If properties is present, analyze them and add them
        if (properties.getLength() > 0) {
            for (int i = 0; i < properties.getLength(); i++) {
                Element elemProperty = (Element) properties.item(i);
                String pName = getAttributeValue(elemProperty, "name");
                String pValue = getAttributeValue(elemProperty, "value");
                if (pName != null && pValue != null) {
                    returnedProperties.setProperty(pName, pValue);
                }

            }
        }
    } else if (list.getLength() > 1) {
        throw new IllegalStateException("Element '" + name + "' on '" + base
                + "' should be unique but there are '" + list.getLength() + "' elements");
    }

    return returnedProperties;
}