Java XML Attribute Get getAttributeValueAsBoolean(Attr attribute)

Here you can find the source of getAttributeValueAsBoolean(Attr attribute)

Description

Parses the attribute's value.

License

Open Source License

Parameter

Parameter Description
attribute attribute whose value will be converted to a boolean

Return

boolean value of the attribute or null

Declaration

public static Boolean getAttributeValueAsBoolean(Attr attribute) 

Method Source Code

//package com.java2s;
/*// w w w.  ja  v  a2s  .c  om
 * Licensed to the University Corporation for Advanced Internet Development, 
 * Inc. (UCAID) under one or more contributor license agreements.  See the 
 * NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The UCAID licenses this file to You under the Apache 
 * License, Version 2.0 (the "License"); you may not use this file except in 
 * compliance with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.w3c.dom.Attr;

public class Main {
    /**
     * Parses the attribute's value. If the value is 0 or "false" then false is returned, if the value is 1 or "true"
     * then true is returned, if the value is anything else then null returned.
     * 
     * @param attribute attribute whose value will be converted to a boolean
     * 
     * @return boolean value of the attribute or null
     */
    public static Boolean getAttributeValueAsBoolean(Attr attribute) {
        if (attribute == null) {
            return null;
        }

        String valueStr = attribute.getValue();
        if (valueStr.equals("0") || valueStr.equals("false")) {
            return Boolean.FALSE;
        } else if (valueStr.equals("1") || valueStr.equals("true")) {
            return Boolean.TRUE;
        } else {
            return null;
        }
    }
}

Related

  1. getAttributeValue(Node sNode, String attribName)
  2. getAttributeValue(NodeList elements, String attributeName)
  3. getAttributeValue(StartElement element, String namespaceURI, String localPart)
  4. getAttributeValue(StartElement startElement, String attributeName)
  5. getAttributeValue(String attributeName, Node xmlNode)
  6. getAttributeValueAsBoolean(Element el, String attrName)
  7. getAttributeValueAsDouble(Node node, String attributeName)
  8. getAttributeValueAsInteger(Node node, String attributeName, Integer defaultValue)
  9. getAttributeValueAsLong(final Element el, final String attrName, final long defaultVal)