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 parentElement, String childName) {
        Element[] list = getChildrenByName(parentElement, childName);
        if (list.length == 0) {
            return null;
        }
        // if (list.length > 1) {
        // throw new IllegalStateException("Too many (" + list.length + ") '"
        // + childName + "' elements found!");
        // }
        return list[0];
    }

    public static Element[] getChildrenByName(Element parentElement, String childrenName) {
        NodeList nl = parentElement.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(childrenName)) {
                list.add(n);
            }
        }
        return list.toArray(new Element[list.size()]);
    }
}