Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;

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

public class Main {
    /**
     * Access all immediate child elements inside the given Element
     *
     * @param element the starting element, cannot be null.
     * @param elemName the name of the child element to look for, cannot be
     * null.
     * @return array of all immediate child element inside element, or
     * an array of size zero if no child elements are found.
     */
    public static Element[] getChildElements(Element element, String elemName) {
        NodeList list = element.getChildNodes();
        int len = list.getLength();
        ArrayList<Node> array = new ArrayList<Node>(len);

        for (int i = 0; i < len; i++) {
            Node n = list.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE) {
                if (elemName.equals(n.getNodeName())) {
                    array.add(n);
                }
            }
        }
        Element[] elems = new Element[array.size()];

        return (Element[]) array.toArray(elems);
    }

    /**
     * Access all immediate child elements inside the given Element
     *
     * @param element the starting element, cannot be null.
     * @param elemName the name of the child element to look for, cannot be
     * null.
     * @return array of all immediate child element inside element, or
     * an array of size zero if no child elements are found.
     */
    public static Element[] getChildElements(Element element) {
        NodeList list = element.getChildNodes();
        int len = list.getLength();
        ArrayList<Node> array = new ArrayList<Node>(len);

        for (int i = 0; i < len; i++) {
            Node n = list.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE) {
                array.add(n);
            }
        }
        Element[] elems = new Element[array.size()];

        return (Element[]) array.toArray(elems);
    }

    /**
     * Access all immediate child elements inside the given Element
     *
     * @param element the starting element, cannot be null.
     * @param namespaceURI name space of the child element to look for,
     * cannot be null. Use "*" to match all namespaces
     * @param elemName the name of the child element to look for,
     * cannot be null.
     * @return array of all immediate child element inside element, or
     * an array of size zero if no child elements are found.
     */
    public static Element[] getChildElements(Element element, String namespaceURI, String elemName) {
        NodeList list = element.getChildNodes();
        int len = list.getLength();
        ArrayList<Node> array = new ArrayList<Node>(len);

        for (int i = 0; i < len; i++) {
            Node n = list.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE) {
                if (elemName.equals(n.getLocalName())
                        && ("*".equals(namespaceURI) || namespaceURI.equals(n.getNamespaceURI()))) {
                    array.add(n);
                }
            }
        }
        Element[] elems = new Element[array.size()];

        return (Element[]) array.toArray(elems);
    }
}