Java XML Attribute from Element getIntAttr(Element elem, String attName)

Here you can find the source of getIntAttr(Element elem, String attName)

Description

Return the value of an integer attribute or zero

License

Open Source License

Parameter

Parameter Description
elem Element
attName Attribute name

Return

the parsed attribute value or zero

Declaration

public static int getIntAttr(Element elem, String attName) 

Method Source Code

//package com.java2s;

import org.w3c.dom.Element;

public class Main {
    /**// w  ww. j ava2 s.c om
     * Return the value of an integer attribute or the default value
     * @param elem Element
     * @param attName Attribute name
     * @param defVal default value
     * @return the parsed attribute value or the default value
     */
    public static int getIntAttr(Element elem, String attName, int defVal) {
        try {
            String val = elem.getAttribute(attName);
            if (val != null) {
                return Integer.parseInt(val);
            }
        } catch (NumberFormatException e) {
            //nothing, just return the default value
        }
        return defVal;
    }

    /**
     * Return the value of an integer attribute or zero
     * @param elem Element
     * @param attName Attribute name
     * @return the parsed attribute value or zero
     */
    public static int getIntAttr(Element elem, String attName) {
        return getIntAttr(elem, attName, 0);
    }
}

Related

  1. getFloatAttribute(Element element, String name)
  2. getFloatAttribute(String name, Element el)
  3. getHeadAttr(Element annotU, String attrName)
  4. getIdAttribute(Element domElement)
  5. getIdAttributeValue(Element elem, String name)
  6. getIntAttribute(Element el, String name)
  7. getIntAttribute(Element elem, String attName, boolean mandatory, int defaultValue)
  8. getIntAttribute(Element elem, String attName, boolean mandatory, int defaultValue)
  9. getIntAttribute(Element element, String attribute)