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;@XStreamImplicitprivate 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 使用XStream解析xml的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的android 使用XStream解析xml的实例全部内容,希望文章能够帮你解决android 使用XStream解析xml的实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)