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

NodegetFirstTextChild(Node n)
Return the first text child node of a given node Returns null if there is no text child node
NodeList children = n.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node currentChild = children.item(i);
    if (children.item(i).getNodeType() == Node.TEXT_NODE) {
        return currentChild;
return null;
...
StringgetFirstTextChild(Node node)
get First Text Child
NodeList nodeList = node.getChildNodes();
if (nodeList != null) {
    for (int index = 0; index < nodeList.getLength(); index++) {
        Node child = nodeList.item(index);
        if (child.getNodeType() == Node.TEXT_NODE) {
            if (child.getNodeValue() != null && child.getNodeValue().trim().length() > 0) {
                return child.getNodeValue();
return null;
ElementgetFirstVisibleChildElement(Node parent)
Finds and returns the first visible child element node.
Node child = parent.getFirstChild();
while (child != null) {
    if (child.getNodeType() == Node.ELEMENT_NODE && !isHidden(child)) {
        return (Element) child;
    child = child.getNextSibling();
return null;
...
StringgetValueOfFirstElementChild(final Element element, final String namespace, final String localname)
get Value Of First Element Child
Node node = getFirstElementChild(element, namespace, localname);
return (node == null) ? null : getNodeValue(node);
voidinsertBeforeFirstChild(Element object, Element attrId)
insert Before First Child
if (object.hasChildNodes()) {
    object.insertBefore(attrId, object.getFirstChild());
} else {
    object.appendChild(attrId);
ElementreadFirstChild(Node parentNode, String nodeName)
read First Child
if (parentNode != null) {
    NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeName().equals(nodeName))
            return (Element) children.item(i);
return null;
...
voidVectoriseFirstChildrenNodes(Node node, Vector elements)
It creates a vector with contents all the first children of a node (without the node itself)
NodeList list = node.getChildNodes();
if (list.getLength() > 0)
    for (int i = 0; i < list.getLength(); i++)
        elements.add(list.item(i));
ElementxmlFirstChild(final Element parent, final String childName, final boolean isThrow)
Xml first child.
final NodeList nodeList = parent.getChildNodes();
if (nodeList == null) {
    if (isThrow) {
        throw new IllegalArgumentException("nodeList == null");
    return null;
final int size = nodeList.getLength();
...