read setting name list from resource with XmlPullParser : XmlPullParser « Development « Android






read setting name list from resource with XmlPullParser

  
import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.res.XmlResourceParser;
import android.util.Log;

class Main {
    private static final String TAG = "Utility";

    public static final String ATTR_NAME = "name";

    public static final String TAG_NAME = "settings";
  /**
   * read setting name list from resource
   * 
   * @return list of setting name
   */
  public static ArrayList<String> getSettingNameList(int resource,
      Context context) {
    XmlResourceParser parser = context.getResources().getXml(resource);
    ArrayList<String> settingNameList = new ArrayList<String>();

    try {
      while (parser.next() != XmlPullParser.END_DOCUMENT) {
        if (parser.getEventType() == XmlPullParser.START_TAG
            && TAG_NAME.equals(parser.getName())) {
          String settingName = parser.getAttributeValue(null,
              ATTR_NAME);
          if (settingName != null) {
            Log.i(TAG, settingName);
            settingNameList.add(settingName);
          }
        }
      }
    } catch (XmlPullParserException e) {
      Log.e(TAG, "Parser Exception : ", e);
    } catch (IOException e) {
      Log.e(TAG, "IOException : ", e);
    }

    parser.close();

    return settingNameList;
  }

}

   
    
  








Related examples in the same category

1.Move To Next Sibling XmlPullParser
2.XmlPullParser Util