Example usage for org.w3c.dom Element getChildNodes

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

Introduction

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

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:XmlHelper.java

/**
 * Returns an iterator over the children of the given element with the given
 * tag name./*from  www. j  a  v  a  2  s  .  c o  m*/
 * 
 * @param element
 *          The parent element
 * @param tagName
 *          The name of the desired child
 * @return An interator of children or null if element is null.
 */
public static Iterator getChildrenByTagName(Element element, String tagName) {
    if (element == null)
        return null;
    // getElementsByTagName gives the corresponding elements in the whole
    // descendance. We want only children

    NodeList children = element.getChildNodes();
    ArrayList goodChildren = new ArrayList();
    for (int i = 0; i < children.getLength(); i++) {
        Node currentChild = children.item(i);
        if (currentChild.getNodeType() == Node.ELEMENT_NODE
                && ((Element) currentChild).getTagName().equals(tagName)) {
            goodChildren.add(currentChild);
        }
    }
    return goodChildren.iterator();
}

From source file:com.omertron.thetvdbapi.tools.DOMHelper.java

/**
 * Gets the string value of the tag element name passed
 *
 * @param element// w w  w .  j  ava 2s.  c om
 * @param tagName
 * @return
 */
public static String getValueFromElement(Element element, String tagName) {
    NodeList elementNodeList = element.getElementsByTagName(tagName);
    if (elementNodeList == null) {
        return "";
    } else {
        Element tagElement = (Element) elementNodeList.item(0);
        if (tagElement == null) {
            return "";
        }

        NodeList tagNodeList = tagElement.getChildNodes();
        if (tagNodeList == null || tagNodeList.getLength() == 0) {
            return "";
        }
        return ((Node) tagNodeList.item(0)).getNodeValue();
    }
}

From source file:ee.ria.xroad.proxyui.WSDLParser.java

private static String getChildValue(String childName, Element element) {
    if (element == null) {
        return null;
    }//from   w ww.j ava2  s . c  o m

    NodeList nodeList = element.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);

        if (childName.equals(node.getLocalName())) {
            return getValue(node);
        }
    }

    return null;
}

From source file:Main.java

/**
 *
 * @param element//from ww  w .  j  a  va2s.c o  m
 * @param attributeName
 * @param matches
 */
private static void removeAttributeLoopElement(Element element, String attributeName, String... matches) {

    if (element.hasAttribute(attributeName)) {
        if (matches(element.getTagName(), matches)) {
            element.removeAttribute(attributeName);
        }
    }

    NodeList nodeList = element.getChildNodes();
    int length = nodeList.getLength();
    for (int seq = 0; seq < length; seq++) {
        Node node = nodeList.item(seq);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (element.hasAttribute(attributeName)) {
                if (matches(element.getTagName(), matches)) {
                    removeAttributeLoopElement((Element) node, attributeName, matches);
                }
            }
        }
    }
}

From source file:Main.java

private static void outputElement(Element node, String indent) {
    System.out.print(indent + "<" + node.getTagName());
    NamedNodeMap nm = node.getAttributes();
    for (int i = 0; i < nm.getLength(); i++) {
        Attr attr = (Attr) nm.item(i);
        System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
    }/*from   w  w  w .  j a v a 2s .  c  om*/
    System.out.print(">");
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++)
        outputloop(list.item(i), indent + TAB);
    System.out.println(indent + "</" + node.getTagName() + ">");
}

From source file:com.weibo.wesync.notify.xml.DomUtils.java

/**
 * Retrieve all child elements of the given DOM element that match any of the given element names. Only look at the
 * direct child level of the given element; do not go into further depth (in contrast to the DOM API's
 * <code>getElementsByTagName</code> method).
 *
 * @param ele         the DOM element to analyze
 * @param childEleNames the child element names to look for
 * @return a List of child <code>org.w3c.dom.Element</code> instances
 * @see org.w3c.dom.Element/*from   w  w  w .j  a v a  2 s  .  co m*/
 * @see org.w3c.dom.Element#getElementsByTagName
 */
public static List<Element> getChildElementsByTagName(Element ele, String[] childEleNames) {
    Assert.notNull(ele, "Element must not be null");
    Assert.notNull(childEleNames, "Element names collection must not be null");
    List<String> childEleNameList = Arrays.asList(childEleNames);
    NodeList nl = ele.getChildNodes();
    List<Element> childEles = new ArrayList<Element>();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element && nodeNameMatch(node, childEleNameList)) {
            childEles.add((Element) node);
        }
    }
    return childEles;
}

From source file:de.codesourcery.utils.xml.XmlHelper.java

public static String getDirectChildValue(Element parent, String childTag, boolean isRequired)
        throws ParseException {

    NodeList list = parent.getChildNodes();

    Node matchingNode = null;/*www  .j  a  v  a 2  s. c  o m*/
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);

        if (n.getNodeName().equals(childTag)) {
            if (matchingNode == null) {
                matchingNode = n;
            } else {
                throw new ParseException(
                        "Node " + parent.getNodeName() + " contains more than one child <" + childTag + "> ?!",
                        -1);
            }
        }
    }

    if (matchingNode == null) {
        if (isRequired) {
            throw new ParseException(
                    "Node " + parent.getNodeName() + " contains no child node <" + childTag + "> ?!", -1);
        }
        return null;
    }

    return getNodeValue(matchingNode, null, isRequired);
}

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));
        }//  w ww.java2 s  . co 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 cloneElementAs(Element srcEl, Document dstDoc, String elName) {
    if (srcEl.getNodeName().equals(elName)) {
        if (srcEl.getOwnerDocument() == dstDoc) {
            return (Element) srcEl.cloneNode(true);
        } else {//w  w w.  j av a 2 s  . c o  m
            return (Element) dstDoc.importNode(srcEl, true);
        }
    } else {
        final Element dstEl = dstDoc.createElement(elName);
        final NodeList srcChildren = srcEl.getChildNodes();
        final int n = srcChildren.getLength();
        for (int i = 0; i < n; ++i) {
            final Node srcChild = srcChildren.item(i);
            final Node dstChild = dstDoc.importNode(srcChild, true);
            dstEl.appendChild(dstChild);
        }
        return dstEl;
    }
}

From source file:com.bluexml.xforms.actions.EnumAction.java

private static void fillValues(String type, Document doc) {
    Element root = doc.getDocumentElement();

    Map<String, String> values = new HashMap<String, String>();
    Map<String, String> keys = new HashMap<String, String>();

    NodeList childNodes = root.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (child instanceof Element) {
            Element element = (Element) child;
            addValue(element, values, keys);
        }/*from ww  w  .j a va2s. c  o m*/
    }

    enumValues.put(type, values);
    enumKeys.put(type, keys);
}