Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*  
 * XMLUtils.java
 *
 * Copyright (C) August Mayer, 2001-2004. All rights reserved.
 * Please consult the Boone LICENSE file for additional rights granted to you.
 *
 * Created on 09. Dezember 2002, 16:31
 */

import org.w3c.dom.*;

public class Main {
    /**
     * Extract the text in an element.
     * Concats data of Text and CDATASection elements.
     */
    public static String getText(Element elem) {

        StringBuffer text = new StringBuffer();
        NodeList l = elem.getChildNodes();
        for (int i = 0; i < l.getLength(); i++) {
            Node n = l.item(i);
            if (n instanceof Text)
                text.append(((Text) n).getData());
            else if (n instanceof CDATASection)
                text.append(((CDATASection) n).getData());
        }
        return text.toString();
    }
}