• 读取 xml 到list


    String fileName = "AdapterModultSetting.xml";

    MainSettingData mainSettingData = null;

    SubSettingData subSettingData = null;

    List<MainSettingData> mainSettingDatas = null;

    List<SubSettingData> subSettingDatas = null;

    //第一步--------读SharedPreferences

    //第一步--------从assets中获得xml数据

    InputStream is = mContext.getResources().getAssets().open(fileName);

    XmlPullParser parse = Xml.newPullParser();

    parse.setInput(is, "utf-8");

    int type = parse.getEventType();

    while (type != XmlPullParser.END_DOCUMENT) {

    switch (type) {

    case XmlPullParser.START_DOCUMENT:

    mainSettingDatas = new ArrayList<MainSettingData>();

    break;

    case XmlPullParser.START_TAG:

    Log.i("测试数据", parse.getName());

    if ("main_menu_data".equals(parse.getName())) {

    mainSettingData = new MainSettingData();

    mainSettingData.setId(Integer.parseInt(parse.getAttributeValue(parse.getNamespace(), "id")));//save parameter

    mainSettingData.setTitle(parse.getAttributeValue(parse.getNamespace(), "title"));

    subSettingDatas = new ArrayList<SubSettingData>();//子菜单

    }

    if (mainSettingData != null) {

    if ("sub_menu_data".equals(parse.getName())) {

    subSettingData = new SubSettingData();

    subSettingData.setId(Integer.parseInt(parse.getAttributeValue(parse.getNamespace(), "id")));

    subSettingData.setType(Integer.parseInt(parse.getAttributeValue(parse.getNamespace(), "type")));

    subSettingData.setSubTitle(parse.getAttributeValue(parse.getNamespace(), "subTitle"));

    subSettingData.setSubState(parse.getAttributeValue(parse.getNamespace(), "subState"));

    subSettingData.setSubValue(Integer.parseInt(parse.getAttributeValue(parse.getNamespace(), "subValue")));

    }

    }

    break;

    case XmlPullParser.END_TAG:

    if ("main_menu_data".equals(parse.getName())) {

    if(subSettingData != null && subSettingDatas != null){

    subSettingDatas.add(subSettingData);

    }

    if (mainSettingData != null && mainSettingDatas != null) {

    if(subSettingDatas != null){

    mainSettingData.setSubSettingDatas(subSettingDatas);//子菜单加入主菜单

    }

    mainSettingDatas.add(mainSettingData);

    }

    }

    break;

    }

    type = parse.next();

    }

    is.close();

    数据在mainSettingDatas中

  • 相关阅读:
    PAT(B) 1037 在霍格沃茨找零钱(Java)
    PAT(B) 1043 输出PATest(Java)统计
    PAT(B) 1063 计算谱半径(Java)
    绘制虚线
    contentMode
    数字签名是什么
    动态设置 button的 name 的话 闪动的问题 解决
    setValuesForKeysWithDictionary 的用法
    获得 当前时间
    iOS 键盘类型
  • 原文地址:https://www.cnblogs.com/sode/p/2167569.html
Copyright © 2020-2023  润新知