Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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 {
    /**
     * This method is used to search all the <code>Element</code> objects with
     * given key in the passed <code>Element</code> object.
     * 
     * @param tagName
     *            Name of the tag as String.
     * @param input
     *            Element object.
     * @return <code>Element[]</code> Returns the array of elements, or an empty
     *         array in case here is no match.
     */
    public static List<Element> getElements(String tagName, Element input) {
        NodeList nodes = input.getElementsByTagName(tagName);

        int len = nodes.getLength();
        List<Element> elt = new ArrayList<Element>(len);
        Node node;
        for (int i = 0; i < len; i++) {
            node = nodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                elt.add((Element) node);
            }
        }

        return elt;
    }
}