Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static ArrayList<Element> getChildNodeListByTagName(Element parent, String tagName) {
        ArrayList<Element> lst = new ArrayList();
        traverseNodes(parent, tagName, lst);
        return lst;
    }

    private static void traverseNodes(Element parent, String tagName, List<Element> lst) {
        if (parent.getTagName().indexOf(tagName) != -1) {
            lst.add(parent);
            return;
        } else {
            NodeList nodeList = parent.getChildNodes();
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node item = nodeList.item(i);
                if (item.getNodeType() == Node.ELEMENT_NODE) {
                    traverseNodes((Element) item, tagName, lst);
                }
            }
        }
    }
}