Java XML Attribute Get getBooleanAttribute(Element elem, String attName, boolean mandatory)

Here you can find the source of getBooleanAttribute(Element elem, String attName, boolean mandatory)

Description

getBooleanAttribute purpose.

License

Open Source License

Parameter

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.

Exception

Parameter Description
SAXException When a child attribute is required and not found.

Return

The value if the attribute was found, the false otherwise.

Declaration

public static boolean getBooleanAttribute(Element elem, String attName, boolean mandatory) throws SAXException 

Method Source Code

//package com.java2s;
/*/*from   w  w  w  . j ava  2s.  c  om*/
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2004-2008, Open Source Geospatial Foundation (OSGeo)
 *    
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

import org.w3c.dom.Attr;

import org.w3c.dom.Element;

import org.xml.sax.SAXException;

public class Main {
    /**
     * getBooleanAttribute 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 false otherwise.
     *
     * @throws SAXException When a child attribute is required and not found.
     */
    public static boolean getBooleanAttribute(Element elem, String attName, boolean mandatory) throws SAXException {
        String value = getAttribute(elem, attName, mandatory);

        return Boolean.valueOf(value).booleanValue();
    }

    /**
     * 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 SAXException When a child attribute is required and not found.
     */
    public static String getAttribute(Element elem, String attName, boolean mandatory) throws SAXException {
        Attr att = elem.getAttributeNode(attName);

        String value = null;

        if (att != null) {
            value = att.getValue();
        }

        if (mandatory) {
            if (att == null) {
                throw new SAXException(
                        "element " + elem.getNodeName() + " does not contains an attribute named " + attName);
            } else if ("".equals(value)) {
                throw new SAXException("attribute " + attName + "in element " + elem.getNodeName() + " is empty");
            }
        }

        return value;
    }
}

Related

  1. getBoolean(Element element, String attribute, boolean defaultValue)
  2. getBooleanAttr(Element element, String name)
  3. getBooleanAttrib(Element element, String attribName, String namespaceURI)
  4. getBooleanAttribute(Element e, String name)
  5. getBooleanAttribute(Element el, String name)
  6. getBooleanAttribute(Element elem, String attName, boolean mandatory, boolean defaultValue)
  7. getBooleanAttribute(Element element, String attributeName)
  8. getBooleanAttribute(Element element, String key, boolean defValue)
  9. getBooleanAttribute(Element element, String name)