Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 Copyright 2014 Michael K Martin
    
 This file is part of Civet.
    
 Civet is free software: you can redistribute it and/or modify
 it under the terms of the Lesser GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 Civet is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the Lesser GNU General Public License
 along with Civet.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

import org.w3c.dom.Element;

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

public class Main {
    /**
     * List all child nodes with name sName
     * NOTE: we assume no same name nodes are nested.
     * @param node
     * @param sName
     * @return Element
     */
    public static ArrayList<Element> listChildElementsByName(Node node, String sName) {
        ArrayList<Element> aNodes = new ArrayList<Element>();
        NodeList nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                if (sName.equals(n.getNodeName())) {
                    aNodes.add((Element) n);
                } else {
                    ArrayList<Element> nextNodes = listChildElementsByName(n, sName);
                    if (nextNodes != null)
                        aNodes.addAll(nextNodes);
                }
            }
            // Don't search anything but elements
        }
        return aNodes;
    }
}