欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

android 使用XStream解析xml的实例

时间:2021-07-11 08:43:46|栏目:Android代码|点击:

1,要解析的xml文件文件

xml="<apps>\n" +
" <app>\n" +
"  <id>1</id>\n" +
"  <name>burn</name>\n" +
"  <version>1.0</version>\n" +
"  <info>\n" +
"   <size>10.5</size>\n" +
"   <message>hhhhhhhh</message>\n" +
"  </info>\n" +
"  <info>\n" +
"   <size>10.5</size>\n" +
"   <message>hhhhhhhh</message>\n" +
"  </info>\n" +
" </app>\n" +
" <app>\n" +
"  <id>2</id>\n" +
"  <name>burn2</name>\n" +
"  <version>2.1</version>\n" +
"  <info>\n" +
"   <size>10.5</size>\n" +
"   <message>hhhhhhhh</message>\n" +
"  </info>\n" +
"  <info>\n" +
"   <size>10.5</size>\n" +
"   <message>hhhhhhhh</message>\n" +
"  </info>\n" +
" </app>\n" +
" <app>\n" +
"  <id>3</id>\n" +
"  <name>burn3</name>\n" +
"  <version>3.2</version>\n" +
"  <info>\n" +
"   <size>10.5</size>\n" +
"   <message>hhhhhhhh</message>\n" +
"  </info>\n" +
"  <info>\n" +
"   <size>10.5</size>\n" +
"   <message>hhhhhhhh</message>\n" +
"  </info>\n" +
" </app>\n" +
"</apps>";

2,新建xml文件对应的封装类

/**
* 
xml数据对应的封装类(注意实体类和xml数据的对应关系,所有标签都得对应)
*/
@XStreamAlias("apps")//配置标签别名
public class AppBean {


@XStreamImplicit//忽略集合根节点
private List<App> app_list;


//必须有无参构造方法
public AppBean() {
}


public AppBean(List<App> app_list) {
this.app_list = app_list;
}


public List<App> getApp_list() {
return app_list;
}


public void setApp_list(List<App> app_list) {
this.app_list = app_list;
}


@XStreamAlias("app")
public static class App{


private long id;
private String name;
private String version;


@XStreamImplicit
private List<Infor> infor_list;


public App() {
}


public App(long id, List<Infor> infor_list, String name, String version) {
this.id = id;
this.infor_list = infor_list;
this.name = name;
this.version = version;
}


public long getId() {
return id;
}


public void setId(long id) {
this.id = id;
}


public List<Infor> getInfor_list() {
return infor_list;
}


public void setInfor_list(List<Infor> infor_list) {
this.infor_list = infor_list;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getVersion() {
return version;
}


public void setVersion(String version) {
this.version = version;
}


@XStreamAlias("info")
public static class Infor{


private String message;
private double size;


public Infor() {
}


public Infor(String message, double size) {
this.message = message;
this.size = size;
}


public String getMessage() {
return message;
}


public void setMessage(String message) {
this.message = message;
}


public double getSize() {
return size;
}


public void setSize(double size) {
this.size = size;
}
}
}
}

3,开始xml解析

XStream xStream=new XStream();
xStream.processAnnotations(AppBean.class);//声明使用注解的类
AppBean ab2= (AppBean) xStream.fromXML(xml);//xml-->Bean,只能解析为对象,不能解析为集合
String xml=xStream.toXML(AppBean.class)//Bean-->xml

上一篇:基于移动端真机调试的图文教程(分享)

栏    目:Android代码

下一篇:Android Service总结及详细介绍

本文标题:android 使用XStream解析xml的实例

本文地址:http://www.codeinn.net/misctech/155971.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有