Java tutorial
//package com.java2s; import org.w3c.dom.Element; public class Main { public static int getOptionalInt(Element element, String attribute, int defaultValue) { if (element.hasAttribute(attribute)) { String s = element.getAttribute(attribute); if (s.startsWith("+")) { s = s.substring(1); } return Integer.parseInt(s); } return defaultValue; } /** * @return true if the specified attribute is present and not empty or null in the element */ public static boolean hasAttribute(Element element, String attribute) { String s = element.getAttribute(attribute); if (s == null || "".equals(s)) { return false; } else { return true; } } }