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

public class Main {
    /**
     * Returns element's TEXT Node
     * 
     * @param element
     *            the element which TEXT node is returned
     * @return TEXT node
     */
    public static Text getElementTextNode(Element element) {
        return (Text) getChildNodeByType(element, Node.TEXT_NODE);
    }

    private static Node getChildNodeByType(Element element, short nodeType) {
        if (element == null) {
            return null;
        }

        NodeList nodes = element.getChildNodes();
        if (nodes == null || nodes.getLength() < 1) {
            return null;
        }

        Node node;
        String data;
        for (int i = 0; i < nodes.getLength(); i++) {
            node = nodes.item(i);
            short type = node.getNodeType();
            if (type == nodeType) {
                if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) {
                    data = ((Text) node).getData();
                    if (data == null || data.trim().length() < 1) {
                        continue;
                    }
                }

                return node;
            }
        }

        return null;
    }
}