Android XML Element Child Get getChildElementByName(Element parent, String name)

Here you can find the source of getChildElementByName(Element parent, String name)

Description

get Child Element By Name

Declaration

public static Element getChildElementByName(Element parent, String name) 

Method Source Code

//package com.java2s;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static Element getChildElementByName(Element parent, String name) {
        List<Element> children = getChildElementsByName(parent, name);
        if (children.size() == 0) {
            return null;
        }//from w  w w . java2s  . com
        if (children.size() > 1) {
            throw new RuntimeException("Unexpected number of '" + name
                    + "' elements " + children.size());
        }
        return children.get(0);
    }

    public static List<Element> getChildElementsByName(Element parent,
            String name) {
        List<Element> result = new ArrayList<Element>();
        NodeList childNodes = parent.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if (child.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            String nodeName = child.getNodeName();
            if (name.equals(nodeName.trim())) {
                result.add((Element) child);
            }
        }
        return result;
    }
}

Related

  1. findChildren(Element parent, String tagName)
  2. findFirstChildElement(Element parent)
  3. firstChildWithName(Element element, String name)
  4. getChild(Element element, String name)
  5. getChildByType(Element element, short nodeType)
  6. getChildValue(Element element, String name)
  7. getFullTextChildrenFromElement(Element element)
  8. printChildElements(Element root, PrintStream out, boolean recurse, String prefix)
  9. valueOfFirstChildWithName(Element element, String name)