Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static String getChildText(Element e, String tagname) {
        return getElementText(getChildByTagName(e, tagname));
    }

    public static String getElementText(Element e) {
        if (e == null) {
            return ("");
        }

        NodeList children = e.getChildNodes();

        if (children == null) {
            return ("");
        }

        StringBuffer text = new StringBuffer();

        int listLength = children.getLength();

        for (int i = 0; i < listLength; i++) {
            Node node = children.item(i);

            int nodeType = node.getNodeType();

            if ((nodeType == Node.TEXT_NODE) || (nodeType == Node.CDATA_SECTION_NODE)) {
                String nodeValue = node.getNodeValue();
                if (nodeValue != null) {
                    text.append(nodeValue);
                }
            }
        }

        return (text.toString().trim());
    }

    public static Element getChildByTagName(Element e, String tagname) {
        if (e == null) {
            return null;
        }

        NodeList children = e.getElementsByTagName(tagname);

        if ((children == null) || (children.getLength() == 0)) {
            return (null);
        }

        return ((Element) children.item(0));
    }
}