Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    /**
     * Collapses a list of CDATASection, Text, and predefined EntityReference
     * nodes into a single string.  If the list contains other types of nodes,
     * those other nodes are ignored.
     */
    public static String getText(NodeList nodeList) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            switch (node.getNodeType()) {
            case Node.CDATA_SECTION_NODE:
            case Node.TEXT_NODE:
                buffer.append(node.getNodeValue());
                break;
            case Node.ENTITY_REFERENCE_NODE:
                if (node.getNodeName().equals("amp"))
                    buffer.append('&');
                else if (node.getNodeName().equals("lt"))
                    buffer.append('<');
                else if (node.getNodeName().equals("gt"))
                    buffer.append('>');
                else if (node.getNodeName().equals("apos"))
                    buffer.append('\'');
                else if (node.getNodeName().equals("quot"))
                    buffer.append('"');
                // Any other entity references are ignored
                break;
            default:
                // All other nodes are ignored
            }
        }
        return buffer.toString();
    }
}