android uses XStream to resolve an instance of xml
- 2021-08-17 01:00:20
- OfStack
1. The xml file to parse
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 new encapsulation class corresponding to xml file
/**
*
xml Encapsulation class corresponding to data ( Note that entity classes and xml The correspondence of data, all labels must correspond )
*/
@XStreamAlias("apps")// Configure label aliases
public class AppBean {
@XStreamImplicit// Ignore the collection root node
private List<App> app_list;
// There must be a parameter-free construction method
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. Start xml parsing
XStream xStream=new XStream();
xStream.processAnnotations(AppBean.class);// Declare classes that use annotations
AppBean ab2= (AppBean) xStream.fromXML(xml);//xml-->Bean Is resolved only as an object, not as a collection
String xml=xStream.toXML(AppBean.class)//Bean-->xml