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

ElementgetFirstChildElementNS(Node parent, String[][] elemNames)
Finds and returns the first child node with the given qualified name.
Node child = parent.getFirstChild();
while (child != null) {
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        for (int i = 0; i < elemNames.length; i++) {
            String uri = child.getNamespaceURI();
            if (uri != null && uri.equals(elemNames[i][0])
                    && child.getLocalName().equals(elemNames[i][1])) {
                return (Element) child;
...
ElementgetFirstChildElementOfName(@Nonnull final Node aStartNode, @Nullable final String sName)
Search all child nodes of the given for the first element that has the specified tag name.
final NodeList aNodeList = aStartNode.getChildNodes();
final int nLen = aNodeList.getLength();
for (int i = 0; i < nLen; ++i) {
    final Node aNode = aNodeList.item(i);
    if (aNode.getNodeType() == Node.ELEMENT_NODE) {
        final Element aElement = (Element) aNode;
        if (aElement.getTagName().equals(sName))
            return aElement;
...
CollectiongetFirstChildElementsByTagName(Node contextNode, String elementName)
Returns list of the first child element retrieved by tag name from the parent node and null otherwise.
Collection<Element> elements = null;
Element result = null;
if (contextNode.getNodeType() == Node.DOCUMENT_NODE) {
    result = ((Document) contextNode).getDocumentElement();
    if (!result.getNodeName().equals(elementName)) {
        result = null;
} else {
...
CollectiongetFirstChildElementsByTagName(Node contextNode, String elementName)
Returns list of the first child element retrieved by tag name from the parent node and null otherwise.
Collection<Element> elements = null;
Element result = null;
if (contextNode.getNodeType() == Node.DOCUMENT_NODE) {
    result = ((Document) contextNode).getDocumentElement();
    if (!result.getNodeName().equals(elementName)) {
        result = null;
} else {
...
ElementgetFirstChildElementWithName(Element elem, String name)
get First Child Element With Name
if (elem == null) {
    return null;
for (Element e = getFirstChildElement(elem); e != null; e = getNextSiblingElement(e)) {
    if (e.getTagName().equals(name)) {
        return e;
return null;
ElementgetFirstChildElmtByTag(String aTagName)
get First Child Elmt By Tag
return getFirstChildElmtByTag(this.getRootElmt(), aTagName);
ElementgetFirstChildElmtByTagAndAttribut(Node aNode, String aTagName, String aAttrId, String aAttrValue)
get First Child Elmt By Tag And Attribut
if (aNode == null || aTagName == null || aAttrId == null || aAttrValue == null) {
    return null;
Node wNode = aNode.getFirstChild();
if (wNode == null || (wNode.getNodeType() == Node.ELEMENT_NODE && wNode.getNodeName().equals(aTagName)
        && aAttrValue.equals(((Element) wNode).getAttribute(aAttrId)))) {
    return (Element) wNode;
} else {
...
ElementgetFirstChildNamed(Element elem, String childName)
get First Child Named
NodeList children = elem.getChildNodes();
final int length = children.getLength();
for (int index = 0; index < length; index++) {
    Node node = children.item(index);
    if ((node instanceof Element) && ((Element) node).getTagName().equals(childName)) {
        return (Element) node;
return null;
NodegetFirstChildNamed(Node node, String name)
Returns the first child of node that has the given name, or null.
return getFirstSiblingNamed(node.getFirstChild(), name);
NodegetFirstChildNode(Node parentNode, String childNodeName)
Returns the first child node of parentNode with the given node name or null if no such child node exists.
NodeList nodes = parentNode.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
    Node n = nodes.item(i);
    if (n.getNodeType() == Node.TEXT_NODE)
        continue;
    if (n.getNodeName().equals(childNodeName))
        return n;
return null;