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

StringfirstChildTextContent(Element element, String name)
first Child Text Content
Element child = firstChild(element, name);
if (child != null) {
    return child.getTextContent();
return null;
NodefirstElementNodeChild(Node n)
first Element Node Child
Node m = n.getFirstChild();
while (m != null && !isElementNode(m))
    m = m.getNextSibling();
return m;
ElementfirstNamedChild(Element el, String lName)
return the first child of the element with given name, or null if there are none
if (el == null)
    return null;
Element fc = null;
if (namedChildElements(el, lName).size() > 0)
    fc = namedChildElements(el, lName).elementAt(0);
return fc;
TextfirstTextChild(Element el)
first Text Child
Text text = null;
for (int i = 0; i < el.getChildNodes().getLength(); i++) {
    Node n = el.getChildNodes().item(i);
    if (n instanceof Text)
        text = (Text) n;
return text;
NodegetDirectChild(Node fNode, String localName, String namespace)
get Direct Child
for (Node currentChild = fNode.getFirstChild(); currentChild != null; currentChild = currentChild
        .getNextSibling()) {
    if (localName.equals(currentChild.getLocalName()) && namespace.equals(currentChild.getNamespaceURI())) {
        return currentChild;
return null;
NodegetDirectChild(Node fNode, String localName, String namespace)
get Direct Child
for (Node currentChild = fNode.getFirstChild(); currentChild != null; currentChild = currentChild
        .getNextSibling()) {
    if ((namespace == null || namespace.equalsIgnoreCase(currentChild.getNamespaceURI()))
            && localName.equalsIgnoreCase(currentChild.getLocalName())) {
        return currentChild;
return null;
...
ElementgetDirectChildElement(Element p_rootElement, String p_elementName)
get Direct Child Element
for (Node node = p_rootElement.getFirstChild(); node != null; node = node.getNextSibling()) {
    if (node.getNodeName().equalsIgnoreCase(p_elementName))
        return (Element) node;
return null;
ArrayListgetDirectChildElements(Element parent)
Returns a collection of direct child Elements
ArrayList<Element> result = new ArrayList<Element>();
for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
    if (child instanceof Element) {
        result.add((Element) child);
return result;
ListgetDirectChildElements(Node fNode, String localName, String namespace)
Gets all direct children with specified localname and namespace.
List<Element> children = new ArrayList<Element>();
for (Node currentChild = fNode.getFirstChild(); currentChild != null; currentChild = currentChild
        .getNextSibling()) {
    if (Node.ELEMENT_NODE == currentChild.getNodeType() && localName.equals(currentChild.getLocalName())
            && namespace.equals(currentChild.getNamespaceURI())) {
        children.add((Element) currentChild);
return children;
ListgetDirectChildElementsByTag(Element node, String tag)
This method returns a list of the direct element node children of this element node with the specified tag.
List<Element> children = new ArrayList<Element>();
Node child = node.getFirstChild();
while (child != null) {
    if (child.getNodeType() == Node.ELEMENT_NODE
            && (child.getNodeName().equals(tag) || tag.equals(DOM_WILDCARD))) {
        children.add((Element) child);
    child = child.getNextSibling();
...