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

ElementgetFirstChildByRegex(Element ele, String pattern)
get First Child By Regex
NodeList nl = ele.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    if (node instanceof Element && getLocalName(node).matches(pattern)) {
        return (Element) node;
return null;
...
ElementgetFirstChildByTagName(Element parent, String tagName)
get First Child By Tag Name
NodeList nodes = parent.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
    Node node = nodes.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(tagName)) {
        return (Element) node;
return null;
...
ElementgetFirstChildByTagName(Node parent, String tagName)
get First Child By Tag Name
List<Element> children = getChildrenByTagName(parent, tagName);
return (children.size() == 0) ? null : children.get(0);
NodegetFirstChildByTagNameNS(Node contextNode, String nsuri, String tag)
returns the first child of the contextnode which has the specified tagname and namespace uri regardless of the depth in the tree.
Node n = null;
if (contextNode.getNodeType() == Node.DOCUMENT_NODE) {
    n = ((Document) contextNode).getDocumentElement();
    if (!(n.getNamespaceURI().equals(nsuri) && n.getNodeName().equals(tag))) {
        n = null;
} else {
    NodeList nodes = ((Element) contextNode).getElementsByTagNameNS(nsuri, tag);
...
NodegetFirstChildByType(Element element, int nodeType)
get First Child By Type
NodeList children = element.getChildNodes();
int childCount = children.getLength();
for (int i = 0; i < childCount; i++) {
    Node child = children.item(i);
    if (child.getNodeType() == nodeType) {
        return child;
return null;
NodegetFirstChildByType(Element element, short nodeType)
get First Child By Type
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
    if (childNodes.item(i).getNodeType() == nodeType) {
        return childNodes.item(i);
return null;
ElementgetFirstChildElement(@Nonnull final Node aStartNode)
Get the first direct child element of the passed element.
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)
        return (Element) aNode;
return null;
...
ElementgetFirstChildElement(Element e)
Get the first child of e which is an element, or null if there is no such element.
Node n = e.getFirstChild();
while (n != null && n.getNodeType() != Node.ELEMENT_NODE) {
    n = n.getNextSibling();
return (Element) n;
ElementgetFirstChildElement(Element e)
Gets the first child element.
if (e != null) {
    NodeList children = e.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) node;
return null;
ElementgetFirstChildElement(Element e)
Get the first child of e which is an element, or null if there is no such element.
Node n = e.getFirstChild();
while (n != null && n.getNodeType() != Node.ELEMENT_NODE) {
    n = n.getNextSibling();
return (Element) n;