Java tutorial
//package com.java2s; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; public class Main { /** * Gets a double value of the given attribute * @param node * @param attrName * @param defaultValue * @return */ public static double getDoubleValue(Node node, String attrName, double defaultValue) { String value = getValue(node, attrName, null); if (value == null) return defaultValue; try { return Double.valueOf(value); } catch (NumberFormatException e) { return defaultValue; } } /** * Gets a string value of the given attribute * @param node * @param attrName * @param defaultValue * @return */ public static String getValue(Node node, String attrName, String defaultValue) { Node tmp; if (node == null) return defaultValue; NamedNodeMap attrs = node.getAttributes(); if (attrs == null) return defaultValue; String value = (tmp = attrs.getNamedItem(attrName)) != null ? tmp.getNodeValue() : null; if (value == null) return defaultValue; return value; } }