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中