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 {
    /**
     * Gets the text value of the given element.
     * 
     * @param e the element.
     * @return the text contents of the given element.s
     */
    public static String getText(Element e) {
        StringBuilder sb = null;

        if (e != null) {
            NodeList children = e.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                Node node = children.item(i);

                switch (node.getNodeType()) {
                case Node.TEXT_NODE:
                case Node.CDATA_SECTION_NODE:
                    if (sb == null) {
                        sb = new StringBuilder();
                    }
                    sb.append(node.getNodeValue());
                    break;
                }
            }
        }

        return (sb != null) ? sb.toString() : null;
    }
}