Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.*;

public class Main {
    /**
     *   Gets the text for a node by concatenating child TEXT elements.
     */
    public synchronized static String getText(Node n) {
        if (n == null)
            throw new IllegalArgumentException("Node argument cannot be null");

        StringBuilder b = new StringBuilder();
        NodeList nl = n.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i).getNodeType() == Node.TEXT_NODE)
                b.append(nl.item(i).getNodeValue());
            else if (nl.item(i).getNodeType() == Node.CDATA_SECTION_NODE)
                b.append(nl.item(i).getNodeValue());
        }

        return b.toString().trim();
    }
}