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 {
    public static String getTextContent(Node e) {
        if (e == null || e.getNodeType() != Node.ELEMENT_NODE) {
            return null;
        }

        NodeList nodes = e.getChildNodes();
        StringBuilder text = new StringBuilder();

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = e.getFirstChild();

            if (node != null && node.getNodeType() == Node.TEXT_NODE) {
                String s = node.getNodeValue();
                if (s != null) {
                    text.append(s);
                }
            }
        }

        if (text.length() > 0) {
            return text.toString();
        } else {
            return null;
        }
    }
}