Get attribute value : Xml « Development « Android






Get attribute value

     

//package org.anddev.andengine.util;

import org.xml.sax.Attributes;

/**
 * (c) 2010 Nicolas Gramlich 
 * (c) 2011 Zynga Inc.
 * 
 * @author Nicolas Gramlich
 * @since 22:02:09 - 21.07.2010
 */
class SAXUtils {
  public static String getAttribute(final Attributes pAttributes, final String pAttributeName, final String pDefaultValue) {
    final String value = pAttributes.getValue("", pAttributeName);
    return (value != null) ? value : pDefaultValue;
  }

  public static String getAttributeOrThrow(final Attributes pAttributes, final String pAttributeName) {
    final String value = pAttributes.getValue("", pAttributeName);
    if(value != null) {
      return value;
    } else {
      throw new IllegalArgumentException("No value found for attribute: '" + pAttributeName + "'");
    }
  }

  public static boolean getBooleanAttribute(final Attributes pAttributes, final String pAttributeName, final boolean pDefaultValue) {
    final String value = pAttributes.getValue("", pAttributeName);
    return (value != null) ? Boolean.parseBoolean(value) : pDefaultValue;
  }

  public static boolean getBooleanAttributeOrThrow(final Attributes pAttributes, final String pAttributeName) {
    return Boolean.parseBoolean(SAXUtils.getAttributeOrThrow(pAttributes, pAttributeName));
  }

  public static byte getByteAttribute(final Attributes pAttributes, final String pAttributeName, final byte pDefaultValue) {
    final String value = pAttributes.getValue("", pAttributeName);
    return (value != null) ? Byte.parseByte(value) : pDefaultValue;
  }

  public static byte getByteAttributeOrThrow(final Attributes pAttributes, final String pAttributeName) {
    return Byte.parseByte(SAXUtils.getAttributeOrThrow(pAttributes, pAttributeName));
  }


  public static short getShortAttribute(final Attributes pAttributes, final String pAttributeName, final short pDefaultValue) {
    final String value = pAttributes.getValue("", pAttributeName);
    return (value != null) ? Short.parseShort(value) : pDefaultValue;
  }

  public static short getShortAttributeOrThrow(final Attributes pAttributes, final String pAttributeName) {
    return Short.parseShort(SAXUtils.getAttributeOrThrow(pAttributes, pAttributeName));
  }


  public static int getIntAttribute(final Attributes pAttributes, final String pAttributeName, final int pDefaultValue) {
    final String value = pAttributes.getValue("", pAttributeName);
    return (value != null) ? Integer.parseInt(value) : pDefaultValue;
  }

  public static int getIntAttributeOrThrow(final Attributes pAttributes, final String pAttributeName) {
    return Integer.parseInt(SAXUtils.getAttributeOrThrow(pAttributes, pAttributeName));
  }


  public static long getLongAttribute(final Attributes pAttributes, final String pAttributeName, final long pDefaultValue) {
    final String value = pAttributes.getValue("", pAttributeName);
    return (value != null) ? Long.parseLong(value) : pDefaultValue;
  }

  public static long getLongAttributeOrThrow(final Attributes pAttributes, final String pAttributeName) {
    return Long.parseLong(SAXUtils.getAttributeOrThrow(pAttributes, pAttributeName));
  }


  public static float getFloatAttribute(final Attributes pAttributes, final String pAttributeName, final float pDefaultValue) {
    final String value = pAttributes.getValue("", pAttributeName);
    return (value != null) ? Float.parseFloat(value) : pDefaultValue;
  }

  public static float getFloatAttributeOrThrow(final Attributes pAttributes, final String pAttributeName) {
    return Float.parseFloat(SAXUtils.getAttributeOrThrow(pAttributes, pAttributeName));
  }


  public static double getDoubleAttribute(final Attributes pAttributes, final String pAttributeName, final double pDefaultValue) {
    final String value = pAttributes.getValue("", pAttributeName);
    return (value != null) ? Double.parseDouble(value) : pDefaultValue;
  }

  public static double getDoubleAttributeOrThrow(final Attributes pAttributes, final String pAttributeName) {
    return Double.parseDouble(SAXUtils.getAttributeOrThrow(pAttributes, pAttributeName));
  }


  public static void appendAttribute(final StringBuilder pStringBuilder, final String pName, final boolean pValue) {
    SAXUtils.appendAttribute(pStringBuilder, pName, String.valueOf(pValue));
  }

  public static void appendAttribute(final StringBuilder pStringBuilder, final String pName, final byte pValue) {
    SAXUtils.appendAttribute(pStringBuilder, pName, String.valueOf(pValue));
  }

  public static void appendAttribute(final StringBuilder pStringBuilder, final String pName, final short pValue) {
    SAXUtils.appendAttribute(pStringBuilder, pName, String.valueOf(pValue));
  }

  public static void appendAttribute(final StringBuilder pStringBuilder, final String pName, final int pValue) {
    SAXUtils.appendAttribute(pStringBuilder, pName, String.valueOf(pValue));
  }

  public static void appendAttribute(final StringBuilder pStringBuilder, final String pName, final long pValue) {
    SAXUtils.appendAttribute(pStringBuilder, pName, String.valueOf(pValue));
  }

  public static void appendAttribute(final StringBuilder pStringBuilder, final String pName, final float pValue) {
    SAXUtils.appendAttribute(pStringBuilder, pName, String.valueOf(pValue));
  }

  public static void appendAttribute(final StringBuilder pStringBuilder, final String pName, final double pValue) {
    SAXUtils.appendAttribute(pStringBuilder, pName, String.valueOf(pValue));
  }

  public static void appendAttribute(final StringBuilder pStringBuilder, final String pName, final String pValue) {
    pStringBuilder.append(' ').append(pName).append('=').append('\"').append(pValue).append('\"');
  }

  //          
  // Inner and Anonymous Classes
  //          
}

   
    
    
    
    
  








Related examples in the same category

1.Using xml resource
2.XML Resource Demo
3.Load style from styles.xml
4.Define PreferenceScreen in xml file
5.Using XML Parser
6.XML-defined adapters can be used to easily create adapters in your own application or to pass adapters to other processes.
7.Xml Parse
8.Xml Serializer Uri
9.Get Text Content from Xml Node
10.Escape un escape Xml
11.Get Xml node value with substring
12.Get value from Element
13.Get field from NamedNodeMap
14.get Node Value With Attribute
15.get Character Data From Element
16.update Xml