Get string value of specified XML attribute. - Java XML

Java examples for XML:XML Attribute

Description

Get string value of specified XML attribute.

Demo Code


//package com.java2s;
import org.w3c.dom.DOMException;
import org.w3c.dom.NamedNodeMap;

public class Main {
    /**// ww w  .j  a  v a  2s  .  c  o  m
     * Get string value of specified attribute. Return null if attribute isn't defined.
     * @param attribs NamedNodeMap
     * @param attributeName String
     * @return String
     * @throws DOMException
     */
    public static String getAttributeValue(NamedNodeMap attribs,
            String attributeName) throws DOMException {
        String value = null;
        if (attribs.getNamedItem(attributeName) != null) {
            value = attribs.getNamedItem(attributeName).getNodeValue();
        }
        return value;
    }

    /**
     * Get string value of specified attribute. If attribute isn't defined return defValue.
     * @param attribs NamedNodeMap
     * @param attributeName String
     * @param defValue String
     * @return String
     * @throws DOMException
     */
    public static String getAttributeValue(NamedNodeMap attribs,
            String attributeName, String defValue) throws DOMException {
        if (attribs.getNamedItem(attributeName) != null) {
            return attribs.getNamedItem(attributeName).getNodeValue();
        } else {
            return defValue;
        }
    }
}

Related Tutorials