android解析XML文件啊
我用pullparser解析器解析XML文件时,出现这样错误,本来按正常的来话在这个文件中月份是不同的,分别是201201,201202,201203,201204.但我...
我用pullparser解析器解析XML文件时,出现这样错误,本来按正常的来话在这个文件中月份是不同的,分别是201201,201202,201203,201204.但我解析出来的时候,全部都是201204,XML文件如下:<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<PersonInfoCache>
<person>
<pension>785.6</pension>
<basemonth>201201</basemonth>
<fund>198.4</fund>
<medical>196.4</medical>
</person>
<person>
<pension>785.6</pension>
<basemonth>201202</basemonth>
<fund>198.4</fund>
<medical>196.4</medical>
</person>
<person>
<pension>785.6</pension>
<basemonth>201203</basemonth>
<fund>198.4</fund>
<medical>196.4</medical>
</person>
<person>
<pension>785.6</pension>
<basemonth>201204</basemonth>
<fund>198.4</fund>
<medical>196.4</medical>
</person>
</PersonInfoCache>
解析代码如下:
public static List<Map<String, Object>> getPersons(InputStream xml) throws Exception{
List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
Map<String,Object> map=new HashMap<String,Object>();
XmlPullParser pullParser=Xml.newPullParser();
pullParser.setInput(xml, "UTF-8");
int event=pullParser.getEventType();
while(event!=XmlPullParser.END_DOCUMENT){
switch (event) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
if("pension".equalsIgnoreCase(pullParser.getName())){
map.put("pension", pullParser.nextText());
}
else if("basemonth".equalsIgnoreCase(pullParser.getName())){
map.put("basemonth", pullParser.nextText());
// SharedPreferences preferences=getSharedPreferences("itcase",);
// Editor editor=preferences.edit();
//System.out.println( pullParser.nextText());
}
else if("fund".equalsIgnoreCase(pullParser.getName())){
map.put("fund", pullParser.nextText());
}
else if("medical".equalsIgnoreCase(pullParser.getName())){
map.put("medical", pullParser.nextText());
}
break;
case XmlPullParser.END_TAG:
if("person".equals(pullParser.getName())){
list.add(map);
}
break;
}
event=pullParser.next();
}
return list;
}
解析裁图如下:,求高手帮忙帮我改改代码,为什么总是解析只是得出201204,为什么之前的201201,201202,201203,都没有。而且得到的也是4个201204呢?求啊,我做这个快做疯了 展开
<PersonInfoCache>
<person>
<pension>785.6</pension>
<basemonth>201201</basemonth>
<fund>198.4</fund>
<medical>196.4</medical>
</person>
<person>
<pension>785.6</pension>
<basemonth>201202</basemonth>
<fund>198.4</fund>
<medical>196.4</medical>
</person>
<person>
<pension>785.6</pension>
<basemonth>201203</basemonth>
<fund>198.4</fund>
<medical>196.4</medical>
</person>
<person>
<pension>785.6</pension>
<basemonth>201204</basemonth>
<fund>198.4</fund>
<medical>196.4</medical>
</person>
</PersonInfoCache>
解析代码如下:
public static List<Map<String, Object>> getPersons(InputStream xml) throws Exception{
List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
Map<String,Object> map=new HashMap<String,Object>();
XmlPullParser pullParser=Xml.newPullParser();
pullParser.setInput(xml, "UTF-8");
int event=pullParser.getEventType();
while(event!=XmlPullParser.END_DOCUMENT){
switch (event) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
if("pension".equalsIgnoreCase(pullParser.getName())){
map.put("pension", pullParser.nextText());
}
else if("basemonth".equalsIgnoreCase(pullParser.getName())){
map.put("basemonth", pullParser.nextText());
// SharedPreferences preferences=getSharedPreferences("itcase",);
// Editor editor=preferences.edit();
//System.out.println( pullParser.nextText());
}
else if("fund".equalsIgnoreCase(pullParser.getName())){
map.put("fund", pullParser.nextText());
}
else if("medical".equalsIgnoreCase(pullParser.getName())){
map.put("medical", pullParser.nextText());
}
break;
case XmlPullParser.END_TAG:
if("person".equals(pullParser.getName())){
list.add(map);
}
break;
}
event=pullParser.next();
}
return list;
}
解析裁图如下:,求高手帮忙帮我改改代码,为什么总是解析只是得出201204,为什么之前的201201,201202,201203,都没有。而且得到的也是4个201204呢?求啊,我做这个快做疯了 展开
展开全部
呵呵 因为Map<String,Object> map=new HashMap<String,Object>(); 这个对象你只new过一次,所以list.add(map) 其实是一直list在add同一个对象,看你的XML,他会add这个map四次,为什么值会是最后一次呢,因为这个map在你循环的时候一直在改变,当你读完最后一个标签的时候,他就会结束,也就是说你add了同样的一个map四次,这个map只会出来201204。修改方法很简单,如下:
case XmlPullParser.END_TAG:
if("person".equals(pullParser.getName())){
list.add(map);
map=new HashMap<String,Object>(); //很重要
}
break;
}
这样应该获得就是四个map了,你也能获得所有解析后的信息了。
case XmlPullParser.END_TAG:
if("person".equals(pullParser.getName())){
list.add(map);
map=new HashMap<String,Object>(); //很重要
}
break;
}
这样应该获得就是四个map了,你也能获得所有解析后的信息了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询