Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Misc-Utils - Miscellaneous Utility Classes
 * Copyright (C) 2007 Newisys, Inc. or its licensors, as applicable.
 * Java is a registered trademark of Sun Microsystems, Inc. in the U.S. or
 * other countries.
 *
 * Licensed under the Open Software License version 3.0 (the "License"); you
 * may not use this file except in compliance with the License. You should
 * have received a copy of the License along with this software; if not, you
 * may obtain a copy of the License at
 *
 * http://opensource.org/licenses/osl-3.0.php
 *
 * This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

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

public class Main {
    /**
     * Returns a String containing the concatenation of text contained in the
     * given node. This includes Text node children, and Text node children of
     * Element nodes and their Element node children.
     * 
     * @param node a Node object
     * @return a concatenation of the descendent text
     */
    public static String getChildrenText(Node node) {
        StringBuffer buf = new StringBuffer();
        getChildrenText(node.getChildNodes(), buf);
        return buf.toString();
    }

    /**
     * Returns a String containing the concatenation of text contained in the
     * given node list. This includes Text nodes in the list, and Text node
     * children of Element nodes and their Element node children.
     * 
     * @param nodeList a NodeList object
     * @return a concatenation of the descendent text
     */
    public static String getChildrenText(NodeList nodeList) {
        StringBuffer buf = new StringBuffer();
        getChildrenText(nodeList, buf);
        return buf.toString();
    }

    private static void getChildrenText(NodeList nodeList, StringBuffer buf) {
        int len = nodeList.getLength();
        for (int i = 0; i < len; ++i) {
            Node child = nodeList.item(i);
            while (child != null) {
                short nodeType = child.getNodeType();
                switch (nodeType) {
                case Node.TEXT_NODE:
                    buf.append(child.getNodeValue());
                    break;
                case Node.ELEMENT_NODE:
                    getChildrenText(child.getChildNodes(), buf);
                    break;
                }
                child = child.getNextSibling();
            }
        }
    }
}