Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    /**
     * Get the text content of an element. If the element contains
     * mixed content (both elements and text), only the first text section
     * is returned.
     *
     * @param element target element to retrieve text on, cannot be null.
     * @return text content of the element.
     */
    public static String getElementText(Element element) {
        element.normalize();
        NodeList list = element.getChildNodes();
        int len = list.getLength();

        for (int i = 0; i < len; i++) {
            Node n = list.item(i);

            if (n.getNodeType() == Node.TEXT_NODE) {
                String s = n.getNodeValue();

                if (s != null) {
                    return s.trim();
                }
            }
        }
        return null;
    }
}