Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2002-2005 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 *******************************************************************************/

import org.w3c.dom.Attr;

import org.w3c.dom.Element;

public class Main {
    /**
     * Get attribute value with the given name defined for the specified element.
     * 
     * @param element an Element object.
     * @param attrName a name of an attribute
     * @return the attribute value.
     */
    public static String getAttributeValue(Element element, String attrName) {
        String attrValue = null;
        Attr attr = null;

        // Get the attribute using its name
        if ((attr = element.getAttributeNode(attrName)) != null) {
            attrValue = attr.getValue().trim();
        }

        // Return attribute value
        return attrValue;
    }

    /**
     * Get attribute value.
     * 
     * @param element an Element object.
     * @param attrName a name of an attribute
     * @param defaultValue a default value for the specified attribute.
     * @return the attribute value if found. Otherwise the specified default
     *         value.
     */
    public static String getAttributeValue(Element element, String attrName, String defaultValue) {
        String returnValue = defaultValue;
        String attrValue = null;

        if ((attrValue = getAttributeValue(element, attrName)) != null)
            returnValue = attrValue;

        return returnValue;
    }
}