java解析通讯录xml_android下通过xstream解析复杂的xml格式信息

在android下通过xstream解析xml格式信息一文中介绍了通过xstream解析比较简单的xml文件到相应的Java bean中,下面介绍带节点的xml文件,xml文件格式如下

ShangHai

上海

ShangHai ...

上海...

解析的代码如下:

1、先创建相应的java bean:

public class CityInfo {

private String id;

private List name=new ArrayList();

private List content=new ArrayList();

private List images=new ArrayList();

相应的set/get方法

public class ItemInfo {

String value;

private String lang;

private String name;

private String src;

相应的set/get方法

2、通过xstream解析xml文件

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

this.textView=(TextView) this.findViewById(R.id.TextView01);

HttpClient client = new DefaultHttpClient();

StringBuilder builder = new StringBuilder();

HttpGet get = new HttpGet("链接地址");

try {

HttpResponse response = client.execute(get);

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();

}

}

源代码:

http://easymorse-android.googlecode.com/svn/trunk/android.readxml/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值