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:Main.java

/**
 * Get all child elements of the specified (root) element, that are named by the specified name. If the root is null,
 * an illegal argument exception is thrown. If the root has no children by the specified name, an empty array is
 * returned.//from w  ww .ja  va 2s . co m
 * 
 * @param root The element to search.
 * @param name The name of the child elements to look for.
 * @return An array of the child elements named by the specified name, of the root.
 */
public static ArrayList<Element> getElements(Element root, String name) {
    if (root == null || name == null || name.length() <= 0)
        throw new IllegalArgumentException("Null or invalid root element or name!");
    NodeList lst = root.getChildNodes();
    int size = lst.getLength();
    ArrayList<Element> a = new ArrayList<Element>(size / 2);
    name = localName(name);
    for (int i = 0; i < size; i++) {
        Node node = lst.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = localName(node);
            if (name.equals(nodeName))
                a.add((Element) node);
        }
    }
    return a;
}

From source file:Main.java

public static List<Element> getAllElementsWithAttrId(final Element element, final String namespace) {
    List<Element> list = new LinkedList<Element>();
    if (elementHasId(element, namespace)) {
        list.add(element);//from   w ww . jav a  2  s .c om
    }

    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Element == false) {
            continue;
        }

        addAllElementsWithAttrId(list, (Element) child, namespace);
    }

    return list;
}

From source file:no.kantega.commons.util.XMLHelper.java

public static void removeChild(Element parent, String name) {
    NodeList children = parent.getChildNodes();
    if (children == null)
        return;/* www . j a v  a  2s. com*/

    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equalsIgnoreCase(name)) {
            parent.removeChild(child);
        }
    }
}

From source file:Main.java

public static Element getFirstChildElementNS(Element elm, String tns, String localName) {
    if (tns == null && localName == null)
        return getFirstChildElement(elm);

    if (tns == null)
        return getFirstChildElement(elm, localName);

    NodeList nl = elm.getChildNodes();
    for (int c = 0; c < nl.getLength(); c++) {
        Node node = nl.item(c);//from   w w  w  .jav a2s  .  c  o m
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (localName == null && tns.equals(node.getNamespaceURI()))
            return (Element) node;

        if (localName != null && tns.equals(node.getNamespaceURI()) && localName.equals(node.getLocalName()))
            return (Element) node;
    }

    return null;
}

From source file:Main.java

private static List<RSSChannel> readChannels(NodeList nodes) {
    List<RSSChannel> result = new ArrayList<RSSChannel>();
    for (int i = 0; i < nodes.getLength(); ++i) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            Element elem = (Element) node;
            if (elem.getTagName().equalsIgnoreCase("channel")) {
                result.add(readChannel(elem.getChildNodes()));
            }/*from  w w  w  .  jav a 2  s . c o m*/
        }
    }
    return result;
}

From source file:Main.java

/**
 * For compatibility reasons the following is required:
 * If the value of a text node is to be changed, but a CDATA section with this name
 * already exists, the CDATA section is removed an a text node is created or changed.
 *
 * If the value of a CDATA section is to be changed, but a text node with this name
 * already exists, the text node is removed an a CDATA section is created or changed.
 *
 */// w ww.j  a  v a  2 s  .  c o m
public static void setElementText(Element e, String newValue, boolean cdata) {
    if (e == null) {
        return;
    }

    Node node = null;

    NodeList children = e.getChildNodes();

    if (children != null) {
        Node childToRemove = null;
        boolean changed = false;

        int listLength = children.getLength();

        for (int i = 0; i < listLength; i++) {
            node = children.item(i);

            int nodeType = node.getNodeType();

            if (nodeType == Node.TEXT_NODE) {
                if (cdata) {
                    childToRemove = node;
                } else {
                    node.setNodeValue(newValue);
                    changed = true;
                }
            }

            if (nodeType == Node.CDATA_SECTION_NODE) {
                if (!cdata) {
                    childToRemove = node;
                } else {
                    node.setNodeValue(newValue);
                    changed = true;
                }

            }
        }

        if (childToRemove != null) {
            // System.out.println("removing child " + childToRemove.getNodeValue());
            childToRemove.setNodeValue("");
            e.removeChild(childToRemove);
        }

        if (changed) {
            return;
        }
    }

    Document doc = e.getOwnerDocument();

    if (cdata) {
        node = doc.createCDATASection(newValue);
    } else {
        node = doc.createTextNode(newValue);
    }

    e.appendChild(node);
}

From source file:no.kantega.commons.util.XMLHelper.java

public static Element getChildByName(Element parent, String name) {
    NodeList children = parent.getChildNodes();
    if (children == null)
        return null;

    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equalsIgnoreCase(name)) {
            return (Element) child;
        }//  www .j a va 2 s  .  c o m
    }

    return null;
}

From source file:Main.java

public static Element getChildWithAttributeValue(Element element, String attributeName, String attributeValue) {

    if (element == null) {
        return null;
    }/*  w ww . j  a v  a2  s . com*/

    NodeList children;

    try {
        children = element.getChildNodes();
    } catch (IllegalArgumentException e) {
        // We've seen this throw a IllegalArgumentException from
        // com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl.getNodeObject(DeferredDocumentImpl.java:1081)
        // under GWT 1.7

        return null;
    }

    int length;

    try {
        length = children.getLength();
    } catch (NullPointerException e) {
        // We've seen this throw a NullPointerException from
        // com.sun.org.apache.xerces.internal.dom.ParentNode.nodeListGetLength(ParentNode.java:696)
        // under GWT 1.7

        return null;
    }

    for (int loop = 0; loop < length; loop++) {
        Node node;

        try {
            node = children.item(loop);
        } catch (NullPointerException e) {
            // We've seen this throw a NullPointerException from
            // com.sun.org.apache.xerces.internal.dom.ParentNode.nodeListItem(ParentNode.java:780)
            // under GWT 1.7

            continue;
        }

        if (!(node instanceof Element)) {
            continue;
        }

        Element child = (Element) node;

        try {
            if (attributeValue.equals(child.getAttribute(attributeName))) {
                return child;
            }
        } catch (NullPointerException e) {
            // We've seen this throw a NullPointerException from
            // com.sun.org.apache.xerces.internal.dom.DeferredAttrNSImpl.synchronizeData(DeferredAttrNSImpl.java:97)
            // under GWT 1.7

            continue;
        }
    }

    return null;
}

From source file:Main.java

/**
 * Returns an iterator over the children of the given element with
 * the given tag name./* w ww  .ja v  a  2  s .  co  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:Main.java

/**
 * This method will find all the parameters under this <code>paramsElement</code> and return them as
 * Map<String, String>. For example,
 * <pre>// w w  w.  ja v  a2  s  .  c  om
 *   <result ... >
 *      <param name="param1">value1</param>
 *      <param name="param2">value2</param>
 *      <param name="param3">value3</param>
 *   </result>
 * </pre>
 * will returns a Map<String, String> with the following key, value pairs :-
 * <ul>
 * <li>param1 - value1</li>
 * <li>param2 - value2</li>
 * <li>param3 - value3</li>
 * </ul>
 *
 * @param paramsElement
 * @return
 */
public static Map<String, String> getParams(Element paramsElement) {
    LinkedHashMap<String, String> params = new LinkedHashMap<String, String>();

    if (paramsElement == null) {
        return params;
    }

    NodeList childNodes = paramsElement.getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);

        if ((childNode.getNodeType() == Node.ELEMENT_NODE) && "param".equals(childNode.getNodeName())) {
            Element paramElement = (Element) childNode;
            String paramName = paramElement.getAttribute("name");

            String val = getContent(paramElement);
            if (val.length() > 0) {
                params.put(paramName, val);
            }
        }
    }

    return params;
}