Java tutorial
//package com.java2s; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; public class Main { /** * Returns the integer value of the named attribute for the given node * If no such attribute exists, returns 0 */ public static int getIntValue(Node node, String name) { // Look for the attribute Node att = get_named_attribute(node, name); if (att != null) { // Return the value return Integer.valueOf(att.getNodeValue()).intValue(); } else { // No such attribute return 0; } } /** * Returns the given attribute of a node, if it exists * Otherwise, returns null */ static Node get_named_attribute(Node node, String att_name) { if (node == null) return null; // Does the node have attributes? NamedNodeMap attributes = node.getAttributes(); if (attributes != null) { // Does the node have this attribute? return attributes.getNamedItem(att_name); } // No attributes return null; } }