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 org.w3c.dom.Element;

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

public class Main {
    public static Element findFirstChildElementByName(Node node, String sName) {
        if (node == null || sName == null)
            return null;
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (sName.equals(node.getNodeName())) {
                return (Element) node;
            } else {
                NodeList nodes = node.getChildNodes();
                for (int i = 0; i < nodes.getLength(); i++) {
                    Node n = nodes.item(i);
                    if (n.getNodeType() == Node.ELEMENT_NODE) {
                        if (sName.equals(n.getNodeName())) {
                            return (Element) n;
                        } else {
                            Element nextNode = findFirstChildElementByName(n, sName);
                            if (nextNode != null)
                                return nextNode;
                        }
                    }
                }
            }
        }
        // Should only reach here if a non-Element node is passed in
        return null;
    }
}