Java Utililty Methods XML Attribute Read

List of utility methods to do XML Attribute Read

Description

The list of methods to do XML Attribute Read are organized into topic(s).

Method

StringgetOptionalAttribute(Element elt, String name)
get Optional Attribute
String value = elt.getAttribute(name);
if (value.equals("")) {
    return null;
} else {
    return value;
StringgetOptionalAttributeValue(NamedNodeMap attrs, String name)
get Optional Attribute Value
Node namedItem = attrs.getNamedItem(name);
if (namedItem == null) {
    return "";
} else {
    return namedItem.getTextContent();
StringreadAttribute(Node element, String attributeName)
read Attribute
if (element == null)
    return null;
NamedNodeMap attributes = element.getAttributes();
if (attributes == null)
    return null;
Node value = attributes.getNamedItem(attributeName);
if (value == null)
    return null;
...
StringreadAttribute(Node node, String attribute, String defaultValue)
Reads the value of an attribute, returning the defaultValue string if not present.
NamedNodeMap attributes = node.getAttributes();
if (null == attributes)
    return defaultValue;
Node attr = attributes.getNamedItem(attribute);
if (null == attr)
    return defaultValue;
return attr.getNodeValue();
StringreadAttributeWithPrefix(Node node, String attributePrefix, String defaultValue)
Reads the value of the first attribute which name matches with the specified attributePrefix.
final NamedNodeMap attributes = node.getAttributes();
if (null == attributes) {
    return defaultValue;
Node attribute;
for (int a = 0; a < attributes.getLength(); a++) {
    attribute = attributes.item(a);
    if (attribute.getNodeName().startsWith(attributePrefix)) {
...
booleanreadBoolAttr(Element element, String attributeName)
read Bool Attr
String attributeValue = element.getAttribute(attributeName);
return Boolean.parseBoolean(attributeValue);
booleanreadBooleanAttribute(Element elem, String name, boolean defaultValue)
read Boolean Attribute
boolean back = defaultValue;
String str = elem.getAttribute(name);
if (str != null) {
    if (str.length() > 0) {
        int hash = str.hashCode();
        if ((hash == "yes".hashCode()) || (hash == "true".hashCode())) {
            back = true;
        } else {
...
booleanreadBooleanAttributeElement(final XMLStreamReader reader, final String attributeName)
Read an element which contains only a single boolean attribute.
requireSingleAttribute(reader, attributeName);
final boolean value = Boolean.parseBoolean(reader.getAttributeValue(0));
requireNoContent(reader);
return value;
floatreadFloat(Node node, String attributeName, float def)
read Float
try {
    return Float.parseFloat(node.getAttributes().getNamedItem(attributeName).getNodeValue());
} catch (Exception ex) {
    return def;
floatreadFloatAttribute(XMLStreamReader reader, String attributeName)
read Float Attribute
return Float.valueOf(reader.getAttributeValue(null, attributeName));