Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.Collection;
import java.util.ArrayList;

public class Main {
    /**
     * This method creates a collection of child nodes from a parent node
     * @param root the parent node
     * @return Collection - childern nodes
     */
    public static Collection getCollection(Node root) {
        Collection collection = new ArrayList();
        if (root != null) {
            NodeList children = root.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                Node child = children.item(i);
                String nodeName = child.getNodeName();
                if (!nodeName.equalsIgnoreCase("#comment") && !nodeName.equalsIgnoreCase("#text")) {
                    if (child.getChildNodes().getLength() > 1) {
                        collection.add(child);
                    } else {
                        Node textChild = child.getFirstChild();
                        if (textChild == null) {
                            // don't accept nulls
                        } else {
                            collection.add(child.getFirstChild().getNodeValue());
                        }
                    }
                }
            }
        }
        return collection;
    }
}