Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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 {
    /**
     * An iterable for all the Element childs of a node
     * 
     * @param n
     *            the node get the children from
     * @return An iterable for all the Element childs of a node
     */
    public static Iterable<Element> elements(Element n) {
        int sz = n.getChildNodes().getLength();
        ArrayList<Element> elements = new ArrayList<Element>(sz);
        for (int idx = 0; idx < sz; idx++) {
            Node node = n.getChildNodes().item(idx);
            if (node instanceof Element)
                elements.add((Element) node);
        }
        return elements;
    }

    /**
     * An Iterable for the Element's childs, with a particular name, of a node
     * 
     * @param n
     *            the node get the children from
     * @param elementName
     *            the name of the child elements
     * @return An Iterable for the Element's children, with a particular name, of a node
     */
    public static List<Element> elements(Element n, String elementName) {
        //        NodeList subNodes = n.getElementsByTagName(elementName);
        NodeList subNodes = n.getChildNodes();
        int sz = subNodes.getLength();
        ArrayList<Element> elements = new ArrayList<Element>(sz);
        for (int idx = 0; idx < sz; idx++) {
            Node node = subNodes.item(idx);
            if (node instanceof Element) {
                Element el = (Element) node;
                if (el.getLocalName().equals(elementName))
                    elements.add((Element) node);
            }
        }
        return elements;
    }
}