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 {
    /**
     * Loads an optional string element value from a XML element
     */
    public static String getStringNode(Element element, String name) {
        return getStringNode(element, name, "");
    }

    /**
     * Loads an optional string element value from a XML element
     */
    public static String getStringNode(Element element, String name, String _default) {
        Element child = getSingleElement(element, name);

        if (child == null)
            return _default;

        String value = child.getTextContent();

        if (value == null)
            return _default;

        return value;
    }

    /**
     * Returns a single element having a given tag
     */
    public static Element getSingleElement(Element element, String tagName) {
        Node node = element.getFirstChild();

        while (node != null) {
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo(tagName) == 0)
                return (Element) node;

            node = node.getNextSibling();
        }

        return null;
    }
}