Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.LinkedList;
import java.util.List;

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

public class Main {
    /**
     * Given an XML element, this method returns nested elements which are direct
     * children of the given element - but only those that have the element-name "childrenName" (the second parameter).
     * @param element
     * @param childrenName
     * @return
     */
    public static List<Element> getChildElements(Element element, String childrenName) {
        List<Element> ret = new LinkedList<Element>();
        NodeList nodeList = element.getChildNodes();
        for (int index = 0; index < nodeList.getLength(); ++index) {
            Node node = nodeList.item(index);
            if (node instanceof Element) {
                Element elementOfNode = (Element) node;
                if (elementOfNode.getTagName().equals(childrenName)) {
                    ret.add(elementOfNode);
                }

            }
        }
        return ret;
    }
}