Java tutorial
//package com.java2s; import org.w3c.dom.*; public class Main { /** Helper method - gets a named attribute's value as a <I>short</I>. @param pNodeMap The <I>NamedNodeMap</I>. @param strName Name of the attribute. @return Value of the attribute. */ public static short getAttributeShort(NamedNodeMap pNodeMap, String strName) throws ClassCastException, NumberFormatException { int nReturn = getAttributeInt(pNodeMap, strName); if (Integer.MIN_VALUE == nReturn) return Short.MIN_VALUE; return (short) nReturn; } /** Helper method - gets a named attribute's value as an <I>int</I>. @param pNodeMap The <I>NamedNodeMap</I>. @param strName Name of the attribute. @return Value of the attribute. */ public static int getAttributeInt(NamedNodeMap pNodeMap, String strName) throws ClassCastException, NumberFormatException { String strValue = getAttributeString(pNodeMap, strName); if (null == strValue) return Integer.MIN_VALUE; return Integer.parseInt(strValue); } /** Helper method - gets a named attribute's value as a <I>String</I>. @param pNodeMap The <I>NamedNodeMap</I>. @param strName Name of the attribute. @return Value of the attribute. */ public static String getAttributeString(NamedNodeMap pNodeMap, String strName) throws ClassCastException { Attr pAttr = getAttribute(pNodeMap, strName); if (null == pAttr) return null; return pAttr.getValue(); } /** Helper methods - gets the <I>Attr</I> <I>Node</I> object from a <I>NamedNodeMap</I> of attributes. @param pNodeMap The <I>NamedNodeMap</I>. @param strName Name of the attribute. @return An <I>Attr</I> object. */ public static Attr getAttribute(NamedNodeMap pNodeMap, String strName) throws ClassCastException { Node pReturn = pNodeMap.getNamedItem(strName); if ((null == pReturn) || (pReturn instanceof Attr)) return (Attr) pReturn; throw new ClassCastException("The node retrieved from the NamedNodeMap is not an Attr object."); } }