Java XML Attribute Get getBooleanAttributeByName(Node content, String attributeName, boolean defaultTrue)

Here you can find the source of getBooleanAttributeByName(Node content, String attributeName, boolean defaultTrue)

Description

get Boolean Attribute By Name

License

Open Source License

Declaration

public static boolean getBooleanAttributeByName(Node content, String attributeName, boolean defaultTrue) 

Method Source Code

//package com.java2s;
/*/*from   ww w  . j  a  va2s .c o  m*/
 * Keystone Development Framework
 * Copyright (C) 2004-2009 Rick Knowles
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * Version 2 as published by the Free Software Foundation.
 *
 * This program 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 General Public License Version 2 for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * Version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

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

public class Main {
    public static boolean getBooleanAttributeByName(Node content, String attributeName, boolean defaultTrue) {
        String value = getAttributeByName(content, attributeName);

        if (defaultTrue) {
            return (value == null) || (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"));
        } else {
            return (value != null) && (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"));
        }
    }

    /**
     * Gets an attribute value by name, returning null if not found
     */
    public static String getAttributeByName(Node content, String attributeName) {
        if (content != null) {
            NamedNodeMap atts = content.getAttributes();

            if (atts == null) {
                return null;
            }

            Node att = atts.getNamedItem(attributeName);

            if (att != null) {
                return att.getNodeValue();
            }
        }

        return null;
    }
}

Related

  1. getBooleanAttribute(Element element, String key, boolean defValue)
  2. getBooleanAttribute(Element element, String name)
  3. getBooleanAttribute(Element element, String name, boolean defaultValue)
  4. getBooleanAttribute(Node n, String attributeName)
  5. getBooleanAttribute(Node targetElem, String keyName, boolean defaultValue)
  6. getBooleanAttributeOption(final Element configuration, final String option, boolean defaultValue)
  7. getBooleanAttributeOptional(Node node, String attributeName, Boolean valueIfEmpty)
  8. getBooleanAttributeRequired(Node node, String attributeName)
  9. getBooleanAttributeValue(Node node, String attribute)