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;

public class Main {
    /**
     * Get the direct text content of an element.
     *
     * @param element
     *            The element.
     *
     * @return The contained text.
     */
    public static String getText(final Element element) {
        final StringBuilder sbuf = new StringBuilder();

        getText(element, sbuf, false);

        return sbuf.toString();
    }

    /**
     * Get the text content of an element.
     *
     * @param element
     *            The element.
     * @param sbuf
     *            The buffer to append to.
     * @param decend
     *            Whether to descend into child elements.
     */
    public static void getText(final Element element, final StringBuilder sbuf, final boolean decend) {
        Node node = element.getFirstChild();

        while (node != null) {
            switch (node.getNodeType()) {
            case Node.TEXT_NODE:
                sbuf.append(node.getNodeValue());
                break;

            case Node.ELEMENT_NODE:
                if (decend) {
                    getText((Element) node, sbuf, decend);
                }

                break;
            }

            node = node.getNextSibling();
        }
    }
}