开发学院手机开发Android 开发 Android 下通过 xstream 解析复杂的 xml 格式信息... 阅读

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.re

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 >

Tags:Android 通过 xstream

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接