Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static Node getFirstElementByTagName(Document doc, String tagName) {
        NodeList list = doc.getElementsByTagName(tagName);
        if (list.getLength() > 0) {
            return list.item(0);
        } else {
            return null;
        }
    }

    public static void getElementsByTagName(Node parent, String tagName, List<Node> result) {
        if (parent == null) {
            return;
        }

        NodeList childs = parent.getChildNodes();
        for (int i = 0; i < childs.getLength(); i++) {
            Node child = childs.item(i);
            if (child.getNodeName().equals(tagName)) {
                result.add(child);
            } else if (child.hasChildNodes()) {
                getElementsByTagName(child, tagName, result);
            }
        }
    }
}