Android 下通过 xstream 解析复杂的 xml 格式信息
2010-04-21 01:31:00 来源:WEB开发网BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
for (String s = reader.readLine(); s != null; s = reader.readLine()) {
builder.append(s);
}
Log.v("response","product:"+builder.toString());
XStream xstream = new XStream();
xstream.alias("city", CityInfo.class);
xstream.alias("item", ItemInfo.class);
xstream.useAttributeFor(CityInfo.class,"id");
xstream.useAttributeFor(ItemInfo.class,"lang");
xstream.useAttributeFor(ItemInfo.class,"name");
xstream.useAttributeFor(ItemInfo.class,"src");
CityInfo cityInfo=(CityInfo) xstream.fromXML(builder.toString());
Log.i("response",xstream.toXML(cityInfo));
textView.setText(((ItemInfo)cityInfo.getName().get(1)).getValue());
} catch (Exception e) {
e.printStackTrace();
}
}
运行结果如下:
04-20 14:03:56.311: INFO/response(16787): < city id="1" >
04-20 14:03:56.311: INFO/response(16787): < content >
04-20 14:03:56.311: INFO/response(16787): < item lang="en" >
04-20 14:03:56.311: INFO/response(16787): < value >ShangHai …< /value >
04-20 14:03:56.311: INFO/response(16787): < /item >
04-20 14:03:56.311: INFO/response(16787): < item lang="zh" >
04-20 14:03:56.311: INFO/response(16787): < value >ä¸æµ·…< /value >
04-20 14:03:56.311: INFO/response(16787): < /item >
04-20 14:03:56.311: INFO/response(16787): < /content >
04-20 14:03:56.311: INFO/response(16787): < images >
04-20 14:03:56.311: INFO/response(16787): < item name="title" src="http://mp.myvsp.cn/images/shanghai.png"/ >
04-20 14:03:56.311: INFO/response(16787): < /images >
更多精彩
赞助商链接