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

ElementgetFirstElementChild(final Element element, final String namespace, final String localname)
get First Element Child
Node node = element.getFirstChild();
if (node == null) {
    return null;
do {
    if (match(node, namespace, localname)) {
        return (Element) node;
} while ((node = node.getNextSibling()) != null);
return null;
NodegetFirstElementChild(final Node node)
Returns the first child node that is an element (type == ELEMENT_NODE).
final NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    final Node child = children.item(i);
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        return child;
return null;
...
ElementgetFirstElementChild(final Node node)
get First Element Child
Element elem = null;
for (Node childNode = node.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
    if (childNode.getNodeType() == Node.ELEMENT_NODE) {
        elem = (Element) childNode;
        return elem;
return elem;
...
ElementgetFirstElementChild(Node root)
get First Element Child
Node node = root.getFirstChild();
while (node != null) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) node;
    node = node.getNextSibling();
return null;
...
StringgetFirstElementContent(Element element, String childName)
get First Element Content
return element.getElementsByTagName(childName).item(0).getTextContent();
StringgetFirstElemText(Element parent, String childName)
Returns text enclosed in the first child element of the parent element
Element[] childs = getChilds(parent, childName);
if (childs.length == 0) {
    return null;
return childs[0].getTextContent();
ElementgetFirstLevelChildElementByTagName(Element parent, String elementName)
Get the immediate child element with a given tag name.
Node childNode = parent.getFirstChild();
while (childNode != null) {
    if ((childNode.getNodeType() == Node.ELEMENT_NODE)
            && ((Element) childNode).getLocalName().equals(elementName)) {
        return (Element) childNode;
    childNode = childNode.getNextSibling();
return null;
ElementgetFirstLevelChildElementByTagName(Element parent, String elementName)
get First Level Child Element By Tag Name
Node childNode = parent.getFirstChild();
while (childNode != null) {
    if ((childNode.getNodeType() == Node.ELEMENT_NODE)
            && ((Element) childNode).getLocalName().equals(elementName)) {
        return (Element) childNode;
    childNode = childNode.getNextSibling();
return null;
ListgetFirstLevelChildElements(Element parent)
get First Level Child Elements
NodeList nodeList = parent.getChildNodes();
ArrayList<Element> childList = new ArrayList<Element>(nodeList.getLength());
Node childNode = parent.getFirstChild();
while (childNode != null) {
    if (childNode.getNodeType() == Node.ELEMENT_NODE) {
        childList.add((Element) childNode);
    childNode = childNode.getNextSibling();
...
ListgetFirstLevelChildElements(Element parent)
Get all the first level child elements.
NodeList nodeList = parent.getChildNodes();
ArrayList<Element> childList = new ArrayList<Element>(nodeList.getLength());
Node childNode = parent.getFirstChild();
while (childNode != null) {
    if (childNode.getNodeType() == Node.ELEMENT_NODE) {
        childList.add((Element) childNode);
    childNode = childNode.getNextSibling();
...