Here you can find the source of getBooleanAttributeOptional(Node node, String attributeName, Boolean valueIfEmpty)
public static Boolean getBooleanAttributeOptional(Node node, String attributeName, Boolean valueIfEmpty) throws Exception
//package com.java2s; //License from project: Apache License import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; public class Main { private static final String VALUE_TRUE = "true"; private static final String VALUE_FALSE = "false"; public static Boolean getBooleanAttributeOptional(Node node, String attributeName, Boolean valueIfEmpty) throws Exception { String string = getStringAttributeOptional(node, attributeName, null); if (string == null) { return valueIfEmpty; }/*from w ww . j av a2 s.c o m*/ if (VALUE_TRUE.equals(string)) { return true; } else if (VALUE_FALSE.equals(string)) { return false; } throw new Exception("Could not read boolean from value '" + string + "' in attribute '" + attributeName + "' in node '" + node.getLocalName() + "'"); } public static String getStringAttributeOptional(Node node, String attributeName, String valueIfEmpty) { NamedNodeMap attributes = node.getAttributes(); Node value = attributes.getNamedItem(attributeName); if (value == null) { return valueIfEmpty; } String text = value.getTextContent(); if (text == null) { return valueIfEmpty; } return text; } }