Java XML Attribute from Element getIntAttribute(Element element, String name, int defaultValue)

Here you can find the source of getIntAttribute(Element element, String name, int defaultValue)

Description

Get the value of an attribute of the given element with the given name as an integer, or return the given default value if the attribute is missing or not parseable as an integer.

License

Open Source License

Parameter

Parameter Description
element the element in which to find the attribute
name the name of the attribute
defaultValue the value to return if the attribute value is not parseable as an integer

Return

the integer value of the attribute, or the default value

Declaration

public static int getIntAttribute(Element element, String name, int defaultValue) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2011 The University of Memphis.  All rights reserved. 
 * This program and the accompanying materials are made available 
 * under the terms of the LIDA Software Framework Non-Commercial License v1.0 
 * which accompanies this distribution, and is available at
 * http://ccrg.cs.memphis.edu/assets/papers/2010/LIDA-framework-non-commercial-v1.0.pdf
 *******************************************************************************/

import org.w3c.dom.Element;

public class Main {
    /**/*w  w  w .j  a v a  2s  .co m*/
     * Get the value of an attribute of the given element with the given name as
     * an integer, or return the given default value if the attribute is missing
     * or not parseable as an integer.
     * 
     * @param element
     *            the element in which to find the attribute
     * @param name
     *            the name of the attribute
     * @param defaultValue
     *            the value to return if the attribute value is not parseable as
     *            an integer
     * @return the integer value of the attribute, or the default value
     */
    public static int getIntAttribute(Element element, String name, int defaultValue) {
        String s = element.getAttribute(name);
        if (s != null) {
            try {
                return Integer.parseInt(s);
            } catch (NumberFormatException e) {
                // ignore
            }
        }
        return defaultValue;
    }
}

Related

  1. getIntAttr(Element elem, String attName)
  2. getIntAttribute(Element el, String name)
  3. getIntAttribute(Element elem, String attName, boolean mandatory, int defaultValue)
  4. getIntAttribute(Element elem, String attName, boolean mandatory, int defaultValue)
  5. getIntAttribute(Element element, String attribute)
  6. getIntAttribute(NamedNodeMap namedNodeMap, String name)
  7. getIntAttributeIgnoreCase(Element ele, String attr, int defaultvalue)
  8. getIntAttributeValue(Element element, String attribute)
  9. getInteger(final org.w3c.dom.Element element, final String attr, int def)