Java Utililty Methods XML Child Get

List of utility methods to do XML Child Get

Description

The list of methods to do XML Child Get are organized into topic(s).

Method

ListgetChildElements(Element parent)
Convenience method for getting all child elements.
return getChildElements(parent, null);
ListgetChildElements(Element parent)
get Child Elements
List<Element> ret = new ArrayList<Element>();
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);
    ret.add(child);
return ret;
ListgetChildElements(Element parentElement)
Returns a list of child nodes of parentElement that are Element s.
List<Element> list = new ArrayList<Element>();
if (parentElement != null) {
    NodeList children = parentElement.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.ELEMENT_NODE)
            list.add((Element) children.item(i));
return list;
ListgetChildElements(Element parentElement)
get Child Elements
NodeList nodeList = parentElement.getChildNodes();
List<Element> childElements = new ArrayList<Element>(nodeList.getLength());
for (int i = 0; i < nodeList.getLength(); i++) {
    Node childNode = nodeList.item(0);
    if (childNode.getNodeType() == Node.ELEMENT_NODE) {
        childElements.add((Element) childNode);
return childElements;
ListgetChildElements(Element parentElement, String childElementTagName)
get Child Elements
List<Element> list = new Vector<Element>();
NodeList childNodes = parentElement.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
    Node node = childNodes.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        if (element.getLocalName().equals(childElementTagName))
            list.add(element);
...
ListgetChildElements(Element parentNode)
return all the child Elements of given Element; it's not deep search, so only the first generation children list is scanned.
if (parentNode == null) {
    return null;
List<Element> resultList = new ArrayList<Element>();
NodeList childNodes = parentNode.getChildNodes();
if (childNodes != null) {
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
...
Element[]getChildElements(Element root, String name, boolean mandatory)
getChildElements purpose.
ArrayList elements = new ArrayList();
Node child = root.getFirstChild();
while (child != null) {
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        if (name.equals(child.getNodeName())) {
            elements.add((Element) child);
    child = child.getNextSibling();
if (mandatory && (elements.isEmpty())) {
    throw new Exception(root.getNodeName() + " does not contains a child element named " + name);
return (Element[]) elements.toArray(new Element[0]);
ListgetChildElements(final Element elem)
get Child Elements
final NodeList list = elem.getChildNodes();
final List<Element> result = Lists.newArrayList();
for (int i = 0; i < list.getLength(); i++) {
    final Node node = list.item(i);
    if (node instanceof Element) {
        result.add((Element) node);
return result;
ListgetChildElements(final Element element)
get Child Elements
final List<Element> elements = new ArrayList<Element>();
final NodeList childs = element.getChildNodes();
for (int i = 0; i < childs.getLength(); i++) {
    Node child = childs.item(i);
    if (child instanceof Element) {
        elements.add((Element) child);
return elements;
ListgetChildElements(final Element element, final String localName)
Retrieves all child Element s of a given local name from a given parent Element as List .
final List<Element> result = new ArrayList<Element>();
for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
    if (!(child instanceof Element)) {
        continue;
    if (!localName.equals(child.getNodeName())) {
        continue;
    result.add((Element) child);
return result;