1. The xml file to be parsed
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. Create a package class corresponding to the new xml file
/** * The encapsulation class corresponding to xml data (note the correspondence between entity class and xml data, all tags must correspond) */ @XStreamAlias("apps")//Configure tag aliaspublic class AppBean { @XStreamImplicit//Ignore the collection root nodeprivate List<App> app_list; //There must be a parameter constructorpublic 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) { = id; this.infor_list = infor_list; = name; = version; } public long getId() { return id; } public void setId(long 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) { = name; } public String getVersion() { return version; } public void setVersion(String version) { = version; } @XStreamAlias("info") public static class Infor{ private String message; private double size; public Infor() { } public Infor(String message, double size) { = message; = size; } public String getMessage() { return message; } public void setMessage(String message) { = message; } public double getSize() { return size; } public void setSize(double size) { = size; } } } }
3. Start xml parsing
XStream xStream=new XStream(); ();//Declare the class using annotationsAppBean ab2= (AppBean) (xml);//xml-->Bean can only be parsed into objects, not as setsString xml=()//Bean-->xml
The above example of Android using XStream to parse xml is all the content I share with you. I hope you can give you a reference and I hope you can support me more.