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;
import org.w3c.dom.Text;

public class Main {
    public static String getTextContent(Node node) {

        if (node == null)
            throw new NullPointerException();

        String textContent;

        textContent = node.getTextContent();
        if (textContent != null)
            return xmlDecode(textContent);

        NodeList childNodes = node.getChildNodes();
        if (childNodes.getLength() > 0 && childNodes.item(0) instanceof Text)
            textContent = node.getNodeValue();
        if (textContent != null)
            return xmlDecode(textContent);

        return "";
    }

    public static String xmlDecode(String string) {

        return string.replace("&amp;", "&").replace("&#39;", "'").replace("&quot;", "\"").replace("&lt;", "<")
                .replace("&gt;", ">");
    }
}