Java XML Attribute Read readInt(Node node, String attributeName, int def)

Here you can find the source of readInt(Node node, String attributeName, int def)

Description

read Int

License

Apache License

Declaration

public static int readInt(Node node, String attributeName, int def) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Node;

public class Main {
    public static int readInt(Node node, String attributeName, int def) {
        try {//  w  w w. j av a  2  s . c o m
            return Integer.parseInt(node.getAttributes().getNamedItem(attributeName).getNodeValue());
        } catch (Exception ex) {
            return def;
        }
    }

    /**
     * Parses an integer from a string, if the string is null returns def.
     *
     * @param i The string to parse
     * @param def The default value if the string is null
     * @return
     * @throws SAXException
     */
    public static int parseInt(String i, int def) {
        if (i == null) {
            return def;
        } else {
            try {
                return Integer.parseInt(i);
            } catch (NumberFormatException ex) {
                return 0;
            }
        }
    }

    public static int parseInt(String i) {
        if (i == null) {
            return 0;
        } else {
            try {
                return Integer.parseInt(i);
            } catch (NumberFormatException ex) {
                return 0;
            }
        }

    }
}

Related

  1. readBoolAttr(Element element, String attributeName)
  2. readBooleanAttribute(Element elem, String name, boolean defaultValue)
  3. readBooleanAttributeElement(final XMLStreamReader reader, final String attributeName)
  4. readFloat(Node node, String attributeName, float def)
  5. readFloatAttribute(XMLStreamReader reader, String attributeName)
  6. readIntAttr(Element element, String attributeName, int defaultValue)
  7. readIntegerAttribute(Element elem, String name, int defaultValue)
  8. readNodeAttributes(Element elem, Object[] map)