Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.*;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static Element getChildElement(Element parent, String tagName) {
        List children = getChildElements(parent, tagName);
        return children.isEmpty() ? null : (Element) children.get(0);
    }

    public static List<Element> getChildElements(Element parent, String tagName) {
        NodeList nodes = parent.getElementsByTagName(tagName);
        ArrayList elements = new ArrayList();

        for (int i = 0; i < nodes.getLength(); ++i) {
            Node node = nodes.item(i);
            if (node instanceof Element && node.getParentNode() == parent) {
                elements.add((Element) node);
            }
        }

        return elements;
    }

    public static List<Element> getChildElements(Element parent) {
        NodeList nodes = parent.getChildNodes();
        ArrayList elements = new ArrayList();

        for (int i = 0; i < nodes.getLength(); ++i) {
            Node node = nodes.item(i);
            if (node instanceof Element && node.getParentNode() == parent) {
                elements.add((Element) node);
            }
        }

        return elements;
    }
}