您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在android中使用XStream對xml進行解析,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
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中使用XStream對xml進行解析就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。