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

ElementgetChild(Element el, String name)
Returns a child element of another element or null if there's no such child.
NodeList nodes = el.getElementsByTagName(name);
if (nodes.getLength() > 0) {
    return (Element) nodes.item(0);
} else {
    return null;
ElementgetChild(Element element, String child)
get Child
NodeList nodes = element.getChildNodes();
Element ret = null;
for (int i = 0; i < nodes.getLength(); i++) {
    Node childNode = nodes.item(i);
    if (childNode.getNodeName().equalsIgnoreCase(child) && childNode.getNodeType() == Node.ELEMENT_NODE) {
        if (ret != null) {
            throw new Exception("Child element '" + child + "' present multiple times");
        } else {
...
ElementgetChild(Element element, String name)
get Child
NodeList nodeList = element.getElementsByTagName(name);
return (Element) nodeList.item(0);
ElementgetChild(Element element, String name)
get Child
String safeName = name.replaceAll("\\$", "::");
for (int i = 0; i < element.getChildNodes().getLength(); i++) {
    Node node = element.getChildNodes().item(i);
    if (node instanceof Element) {
        Element el = (Element) node;
        if (el.getNodeName().equals(safeName))
            return el;
return null;
ElementgetChild(Element element, String name)
Get the first child element with the given name.
return (Element) element.getElementsByTagName(name).item(0);
ElementgetChild(Element node, String tagName)
get Child
Element element = null;
do {
    if (node == null) {
        break;
    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
...
ElementgetChild(Element parent, int i)
get Child
NodeList children = parent.getChildNodes();
if (children.getLength() == 0)
    return null;
int count = 0;
for (int k = 0; k < children.getLength(); k++) {
    Node kid = children.item(k);
    if (kid instanceof Element) {
        if (count == i)
...
ElementgetChild(Element parent, String element)
get Child
NodeList list = parent.getElementsByTagName(element);
return (Element) list.item(0);
ElementgetChild(Element parent, String name)
get Child
for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
    if (child instanceof Element && name.equals(child.getNodeName())) {
        return (Element) child;
return null;
ElementgetChild(Element parent, String name)
get Child
NodeList children = parent.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node n = (Node) children.item(i);
    if (n.getNodeType() == Node.ELEMENT_NODE) {
        Element elem = (Element) n;
        if (elem.getTagName().equals(name)) {
            return elem;
return null;