Java examples for XML:XML Attribute
Get value of specified XML attribute as integer.
//package com.java2s; import org.w3c.dom.DOMException; import org.w3c.dom.NamedNodeMap; public class Main { /**// ww w. j av a 2 s . c om * Get value of specified attribute as integer. If attribute isn't defined return defValue. * @param attribs NamedNodeMap * @param attributeName String * @param defValue int * @return int * @throws DOMException */ public static int getAttributeValueAsInt(NamedNodeMap attribs, String attributeName, int defValue) throws DOMException { String v = getAttributeValue(attribs, attributeName); int result = defValue; if (v != null) { try { result = Integer.parseInt(v); } catch (NumberFormatException ex) { } } return result; } /** * 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; } } }