Here you can find the source of getIntAttribute(Element elem, String attName, boolean mandatory, int defaultValue)
Parameter | Description |
---|---|
elem | The root element to look for children in. |
attName | The name of the attribute to look for. |
mandatory | true when an exception should be thrown if the attribute element does not exist. |
defaultValue | a default value to return incase the attribute was not found. mutually exclusive with the Exception thrown. |
Parameter | Description |
---|---|
Exception | When a attribute element is required andnot found. |
public static int getIntAttribute(Element elem, String attName, boolean mandatory, int defaultValue) throws Exception
//package com.java2s; /* (c) 2014 Open Source Geospatial Foundation - all rights reserved * (c) 2001 - 2013 OpenPlans// www.j a v a2 s . c om * This code is licensed under the GPL 2.0 license, available at the root * application directory. */ import org.w3c.dom.Attr; import org.w3c.dom.Element; public class Main { /** * getIntAttribute purpose. * * <p> * Used to help with XML manipulations. Returns the first child integer * attribute of the specified name. An exception occurs when the node is * required and not found. * </p> * * @param elem The root element to look for children in. * @param attName The name of the attribute to look for. * @param mandatory true when an exception should be thrown if the * attribute element does not exist. * @param defaultValue a default value to return incase the attribute was * not found. mutually exclusive with the Exception * thrown. * * @return The int value if the attribute was found, the default otherwise. * * @throws Exception When a attribute element is required and * not found. */ public static int getIntAttribute(Element elem, String attName, boolean mandatory, int defaultValue) throws Exception { String attValue = getAttribute(elem, attName, mandatory); if (!mandatory && (attValue == null)) { return defaultValue; } try { return Integer.parseInt(attValue); } catch (Exception ex) { if (mandatory) { throw new Exception(attName + " attribute of element " + elem.getNodeName() + " must be an integer, but it's '" + attValue + "'"); } else { return defaultValue; } } } /** * getIntAttribute purpose. * * <p> * Used to help with XML manipulations. Returns the first child integer * attribute of the specified name. An exception occurs when the node is * required and not found. * </p> * * @param elem The root element to look for children in. * @param attName The name of the attribute to look for. * @param mandatory true when an exception should be thrown if the * attribute element does not exist. * * @return The value if the attribute was found, the null otherwise. * * @throws Exception When a child attribute is required and * not found. * @throws NullPointerException DOCUMENT ME! */ public static String getAttribute(Element elem, String attName, boolean mandatory) throws Exception { if (elem == null) { if (mandatory) { throw new NullPointerException(); } return ""; } Attr att = elem.getAttributeNode(attName); String value = null; if (att != null) { value = att.getValue(); } if (mandatory) { if (att == null) { throw new Exception("element " + elem.getNodeName() + " does not contains an attribute named " + attName); } else if ("".equals(value)) { throw new Exception("attribute " + attName + "in element " + elem.getNodeName() + " is empty"); } } return value; } }