您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)怎么在android中使用XStream對(duì)xml進(jìn)行解析,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
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文件對(duì)應(yīng)的封裝類
/** * xml數(shù)據(jù)對(duì)應(yīng)的封裝類(注意實(shí)體類和xml數(shù)據(jù)的對(duì)應(yīng)關(guān)系,所有標(biāo)簽都得對(duì)應(yīng)) */ @XStreamAlias("apps")//配置標(biāo)簽別名 public class AppBean { @XStreamImplicit//忽略集合根節(jié)點(diǎn) private List<App> app_list; //必須有無參構(gòu)造方法 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,只能解析為對(duì)象,不能解析為集合 String xml=xStream.toXML(AppBean.class)//Bean-->xml
關(guān)于怎么在android中使用XStream對(duì)xml進(jìn)行解析就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。