Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static Node getFirstElementByTagName(Node parentNode, String tagName) {
        Node element = null;

        if (!isElement(parentNode)) {
            return null;
        } else {
            if (parentNode.getNodeName().equals(tagName)) {
                return parentNode;
            } else {
                NodeList childList = parentNode.getChildNodes();
                for (int nodeIx = 0; nodeIx < childList.getLength(); nodeIx++) {
                    Node node = childList.item(nodeIx);
                    element = getFirstElementByTagName(node, tagName);
                    if (element != null) {
                        return element;
                    }
                }
            }
        }

        return null;
    }

    public static boolean isElement(Node node) {
        return node != null
                && (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE);
    }
}