Java Utililty Methods XML Node Parent

List of utility methods to do XML Node Parent

Description

The list of methods to do XML Node Parent are organized into topic(s).

Method

ElementgetFirstElement(Node element)
Gets the first element amongst children.
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
    Node item = childNodes.item(i);
    if (item instanceof Element) {
        return (Element) item;
return null;
...
ElementgetFirstElement(Node node, String namespace, String name)
Get the first element matched with the given namespace and name.
NodeList children = node.getChildNodes();
if (children == null)
    return null;
for (int i = 0; i < children.getLength(); i++) {
    try {
        Element child = (Element) children.item(i);
        if (name.equals(getElementLocalName(child)) && namespace.equals(getElementNamespaceURI(child))) {
            return child;
...
NodegetFirstElement(Node xml)
get First Element
if (Node.DOCUMENT_NODE == xml.getNodeType()) {
    xml = xml.getFirstChild();
if (Optional.ofNullable(xml.getNamespaceURI()).orElse("").trim().equals("")) {
    throw new IllegalArgumentException("Namespace URI cannot be null!");
return xml;
ElementgetFirstElementByTagName(Node node, String tagName)
get First Element By Tag Name
ArrayList<Element> elements = getElementsByTagName(node, tagName);
if (!elements.isEmpty())
    return elements.get(0);
return null;
ElementgetFirstElementByTagName(Node node, String tagName)
get First Element By Tag Name
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
    Node n = node.getChildNodes().item(i);
    if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
        return (Element) n;
return null;
NodegetFirstElementWithTagName(Node node, String element)
Returns the first child element found with the specified tag name
if (node != null && element != null) {
    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(element))
            return n;
return null;
NodegetFirstNamedNode(Node node, String name)
get First Named Node
if (node.getNodeName().equalsIgnoreCase(name))
    return node;
else {
    NodeList list = node.getChildNodes();
    for (int i = 0, len = list.getLength(); i < len; i++) {
        Node n = getFirstNamedNode(list.item(i), name);
        if (n != null)
            return n;
...
NodegetFirstNodeByName(Node root, String tagName)
get First Node By Name
NodeList nodes;
nodes = root.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
    Node n = nodes.item(i);
    if (n.getNodeName().equals(tagName)) {
        return n;
return null;
NodegetFirstValidNode(Node node)
get First Valid Node
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    if (isValidNode(children.item(i)))
        return children.item(i);
return null;
ElementgetParent(Element elem)
get Parent
Node parent = elem.getParentNode();
if (parent instanceof Element)
    return (Element) parent;
return null;