Java XML Child Get by Name getChildElement(Element parent, String tagName)

Here you can find the source of getChildElement(Element parent, String tagName)

Description

get Child Element

License

Apache License

Declaration

public static Element getChildElement(Element parent, String tagName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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 getChildElement(Element parent, String tagName) {
        List<Element> children = getChildElements(parent, tagName);
        if (children.isEmpty()) {
            return null;
        }//from   w  w w.j  av  a2s  .c om
        return (Element) children.get(0);
    }

    public static List<Element> getChildElements(Element parent, String tagName) {
        NodeList nodes = parent.getElementsByTagName(tagName);
        List<Element> elements = new ArrayList();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (((node instanceof Element)) && (node.getParentNode() == parent)) {
                elements.add((Element) node);
            }
        }
        return elements;
    }
}

Related

  1. getChildElement(Element parent, String name)
  2. getChildElement(Element parent, String tagName)
  3. getChildElement(Element parent, String tagName)
  4. getChildElement(Element parent, String tagName)
  5. getChildElement(Element parent, String tagName)
  6. getChildElement(Element parentElement, String childElementName)
  7. getChildElement(Element parentElement, String name)
  8. getChildElement(Element parentElement, String tagName)
  9. getChildElement(Element root, String name)