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.HashSet;
import java.util.List;

import java.util.Set;

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

public class Main {
    /**
     * Get the elements who's tags match those passed in. 
     * @param nodeList - the node list in which to look for elements 
     * @param tags - the tags to match (empty returns all elements).
     * @return The elements that matched.
     */
    public static List<Element> getElements(NodeList nodeList, String... tags) {
        int nNodes = nodeList.getLength();
        List<Element> elements = new ArrayList<Element>(nNodes);

        Set<String> tagSet = new HashSet<String>(tags.length);
        for (String tag : tags) {
            tagSet.add(tag);
        }
        for (int i = 0; i < nNodes; ++i) {
            Node node = nodeList.item(i);
            if (node instanceof Element) {
                Element element = (Element) node;
                String tagName = element.getTagName();
                if (tagSet.isEmpty() || tagSet.contains(tagName)) {
                    elements.add(element);
                }
            }
        }
        return elements;
    }
}