Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Creative Commons License 

import org.w3c.dom.Node;

public class Main {
    /**
     * Returns an attribute value of a specified node. If this attribute does
     * not exits an empty string will be returned.
     * 
     * @param node The node containing the attribute.
     * @param attribute The name of the attribute.
     * @return The attribute value.
     */
    public static String getAttributeValue(Node node, String attribute) {
        return getAttributeValue(node, attribute, "");
    }

    /**
     * Returns an attribute value of a specified node. If this attribute does
     * not exits a specified default value will be returned.
     * 
     * @param node The node containing the attribute.
     * @param attribute The name of the attribute.
     * @param defaultValue The value returned if the attribute does not exist.
     * @return The attribute value.
     */
    public static String getAttributeValue(Node node, String attribute, String defaultValue) {
        try {
            return node.getAttributes().getNamedItem(attribute).getTextContent();
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * Returns the text content of a node or an empty string if an error occurs.
     * 
     * @param node The node.
     * @return The text content or an empty string.
     */
    public static String getTextContent(Node node) {
        return getTextContent(node, "");
    }

    /**
     * Returns the text content of a node or an empty string if an error occurs.
     * 
     * @param node The node.
     * @param defaultValue The default value.
     * @return The text content or the default value.
     */
    public static String getTextContent(Node node, String defaultValue) {
        try {
            String content = null;
            if (node != null) {
                content = node.getTextContent();
            }
            return content != null ? content : defaultValue;
        } catch (Exception e) {
            return defaultValue;
        }
    }
}