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

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

Description

get Child Element

License

Open Source License

Parameter

Parameter Description
parent the parent XML element
childName the child node name

Return

the child XML element with specified node name or null if does not exist

Declaration

public static Element getChildElement(final Element parent, final String childName) 

Method Source Code

//package com.java2s;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Main {
    /**/*  www. jav  a  2  s  .  com*/
     * @param parent the parent XML element
     * @param childName the child node name
     * @return the child XML element with specified node name or <code>null</code> if does not exist
     */
    public static Element getChildElement(final Element parent, final String childName) {

        Element child = null;
        if (parent != null) {
            NodeList children = parent.getElementsByTagName(childName);
            if (children.getLength() > 0) {
                child = (Element) children.item(0);
            }
        }
        return child;
    }
}

Related

  1. getChildElement(Element parentElement, String name)
  2. getChildElement(Element parentElement, String tagName)
  3. getChildElement(Element root, String name)
  4. getChildElement(Element root, String name, boolean mandatory)
  5. getChildElement(final Element element, final String namespace, final String tagName)
  6. getChildElement(final Element parent, final String ns, final String localName)
  7. getChildElement(final Node parent, final String name)
  8. getChildElement(final Node parentNode, final String childNodeName)
  9. getChildElement(Node node)