Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.*;

import org.w3c.dom.*;

public class Main {
    public static String getTextContent(Node node) {
        return getTextContent(node, true);
    }

    public static String getTextContent(Node node, boolean trim) {
        if (node == null) {
            return "";
        }

        String textContent = "";
        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node childNode = childNodes.item(i);
            if (childNode.getNodeType() == Node.TEXT_NODE) {
                textContent += childNode.getNodeValue().trim();
            }
        }

        textContent = textContent.replace("\r", "");
        if (textContent.startsWith("\n")) {
            textContent = textContent.substring(1);
        }

        if (trim) {
            textContent = textContent.trim();
        }

        return textContent;
    }

    public static List<Node> getChildNodes(Node node, String... elements) {
        List<Node> result = new ArrayList<Node>();
        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node childNode = childNodes.item(i);
            for (String element : elements) {
                if (element.equals(childNode.getNodeName())) {
                    result.add(childNode);
                }
            }
        }
        return result;
    }
}