Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    public static Element getFirstElementByTagName(Node node, String tagName) {
        ArrayList<Element> elements = getElementsByTagName(node, tagName);
        if (!elements.isEmpty())
            return elements.get(0);
        return null;
    }

    public static ArrayList<Element> getElementsByTagName(Node node, String tagName) {
        ArrayList<Element> elements = new ArrayList<Element>();
        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)) {
                elements.add((Element) n);
            }
        }
        return elements;
    }
}