Java Utililty Methods XML First Child Element

List of utility methods to do XML First Child Element

Description

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

Method

StringgetDirectChildElementValue(Element p_rootElement, String p_elementName)
get Direct Child Element Value
Element element = getDirectChildElement(p_rootElement, p_elementName);
if (element == null)
    return null;
return getElementValue(element);
ListgetDirectChildElementValues(Element p_rootElement, String p_elementName)
get Direct Child Element Values
List result = new ArrayList();
List elems = getChildElements(p_rootElement, p_elementName);
for (Iterator itor = elems.iterator(); itor.hasNext();) {
    Element elem = (Element) itor.next();
    result.add(getElementValue(elem));
return result;
ArrayListgetDirectChildNamedElements(Element parent, String name)
Returns a collection of direct child Elements that match the specified tag name
ArrayList<Element> result = new ArrayList<Element>();
for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
    if (child instanceof Element && name.equals(child.getNodeName())) {
        result.add((Element) child);
return result;
NodegetDirectChildNode(Node node, String name)
get Direct Child Node
Node targetNode = null;
List<Node> nodes = getDirectChildNodes(node, name);
if (nodes.size() == 1) {
    targetNode = nodes.get(0);
return targetNode;
ListgetDirectChildNodes(final Node parent, final String... nodeNames)
Scans a node for directly related child nodes of a particular type.
return getChildNodes(parent, false, nodeNames);
ListgetDirectChildNodes(Node node, String name)
get Direct Child Nodes
LinkedList<Node> nodes = new LinkedList<Node>();
NodeList childList = node.getChildNodes();
for (int childIndex = 0; childIndex < childList.getLength(); childIndex++) {
    Node child = childList.item(childIndex);
    if (child.getNodeName().equals(name)) {
        nodes.add(child);
return nodes;
ElementgetFirstChild(Element e)
get First Child
if (e == null)
    return null;
Node n = e.getFirstChild();
while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
    n = n.getNextSibling();
return (Element) n;
ElementgetFirstChild(Element e, String nsUri, String local)
Gets the first child of the given name, or null.
for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) {
    if (n.getNodeType() == Node.ELEMENT_NODE) {
        Element c = (Element) n;
        if (c.getLocalName().equals(local) && c.getNamespaceURI().equals(nsUri))
            return c;
return null;
...
ElementgetFirstChild(Element elem, String childTag)
get First Child
if (elem.hasChildNodes()) {
    NodeList list = elem.getElementsByTagName(childTag);
    int count = list.getLength();
    for (int i = 0; i < count; i++) {
        Node node = list.item(i);
        if (node.getParentNode() != elem)
            continue;
        if (node.getNodeType() == Node.ELEMENT_NODE) {
...
ElementgetFirstChild(Element element)
get First Child
NodeList nodelist = element.getChildNodes();
for (int i = 0; i < nodelist.getLength(); i++) {
    Node node = nodelist.item(i);
    if (node instanceof Element)
        return (Element) node;
return null;