Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

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

public class Main {
    /**
     * Get all the direct children elements of an element.
     *
     * @param parent
     *            The parent element.
     *
     * @return A list of Element's.
     */
    public static List<Element> getElements(final Element parent) {
        final LinkedList<Element> list = new LinkedList<Element>();

        Node node = parent.getFirstChild();

        while (node != null) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                list.add((Element) node);
            }

            node = node.getNextSibling();
        }

        return list;
    }

    /**
     * Get all the direct children elements of an element that have a specific
     * tag name.
     *
     * @param parent
     *            The parent element.
     * @param name
     *            The tag name to match.
     *
     * @return A list of Element's.
     */
    public static List<Element> getElements(final Element parent, final String name) {
        final LinkedList<Element> list = new LinkedList<Element>();

        Node node = parent.getFirstChild();

        while (node != null) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                final Element element = (Element) node;

                if (element.getTagName().equals(name)) {
                    list.add(element);
                }
            }

            node = node.getNextSibling();
        }

        return list;
    }
}