Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.*;

import java.util.LinkedList;
import java.util.List;

public class Main {
    public static Element getFirstChildElementByTagName(Element element, String name) {

        List<Element> firstChildElementsByTagName = getChildElementsByTagName(element, name);

        if (firstChildElementsByTagName.size() > 0) {
            return firstChildElementsByTagName.get(0);
        } else {
            return null;
        }

    }

    public static List<Element> getChildElementsByTagName(Element element, String name) {

        List<Element> elements = new LinkedList<Element>();

        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) node;
                if (e.getTagName().equals(name)) {
                    elements.add(e);
                }
            }
        }

        return elements;
    }
}