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 {
    public static String findTextValue(Element context, String nodeName, String defaultValue) {
        return getTextValue((Element) findNode(context, nodeName), defaultValue);
    }

    public static String getTextValue(Element e) {
        NodeList childs = e.getChildNodes();
        for (int k = 0; k < childs.getLength(); k++) {
            Node n = childs.item(k);
            if (n.getNodeType() == 3) {
                return n.getNodeValue();
            }
        }
        return null;
    }

    public static String getTextValue(Element value, String defaultValue) {
        if (value == null) {
            return defaultValue;
        }

        String strValue = getTextValue(value);
        if (strValue.isEmpty()) {
            return defaultValue;
        }
        return strValue;
    }

    public static Node findNode(Node node, String nodeName) {
        if (node == null) {
            return null;
        }

        if (node.getNodeName().equals(nodeName)) {
            return node;
        }
        NodeList nodeList = node.getChildNodes();
        int i = 0;
        for (int cnt = nodeList.getLength(); i < cnt; i++) {
            Node child = findNode(nodeList.item(i), nodeName);
            if (child != null) {
                return child;
            }
        }
        return null;
    }
}