Java XML Attribute from Element getIntAttributeIgnoreCase(Element ele, String attr, int defaultvalue)

Here you can find the source of getIntAttributeIgnoreCase(Element ele, String attr, int defaultvalue)

Description

get Int Attribute Ignore Case

License

Open Source License

Parameter

Parameter Description
ele a parameter
attr a parameter
defaultvalue a parameter

Return

the integer attribute of ele called attr. Default value is returned if the attribute is not found.

Declaration

public static int getIntAttributeIgnoreCase(Element ele, String attr, int defaultvalue) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w ww .j  ava  2  s  . c  o m*/
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    /**
     * @param ele 
     * @param attr 
     * @param defaultvalue 
     * @return the integer attribute of ele called attr.  Default value
     * is returned if the attribute is not found.
     */
    public static int getIntAttributeIgnoreCase(Element ele, String attr, int defaultvalue) {
        if (ele == null) {
            return defaultvalue;
        }
        String attrvalue = getAttributeIgnoreCase(ele, attr);
        if (attrvalue == null) {
            return defaultvalue;
        }
        try {
            return Integer.parseInt(attrvalue);
        } catch (NumberFormatException ex) {
            return defaultvalue;
        }
    }

    /**
     * @param element
     * @param string
     * @return the attribute named string on element ignoring case in the comparison
     * or null if not found
     */
    public static String getAttributeIgnoreCase(Element element, String string) {
        NamedNodeMap map = element.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            Node attr = map.item(i);
            if (string.equalsIgnoreCase(attr.getNodeName())) {
                return attr.getNodeValue();
            }
        }
        return null;
    }
}

Related

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