Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.LinkedList;

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

public class Main {

    public static Element getChildByName(Element e, String name) {
        Element[] list = getChildrenByName(e, name);
        if (list.length == 0) {
            return null;
        }
        if (list.length > 1) {
            throw new IllegalStateException("Too many (" + list.length + ") '" + name + "' elements found!");
        }
        return list[0];
    }

    public static Element[] getChildrenByName(Element e, String name) {
        NodeList nl = e.getChildNodes();
        int max = nl.getLength();
        LinkedList<Node> list = new LinkedList<Node>();
        for (int i = 0; i < max; i++) {
            Node n = nl.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
                list.add(n);
            }
        }
        return list.toArray(new Element[list.size()]);
    }
}