<?xml version="1.0" encoding="UTF-8"?>
<data>
<head>
<ver>协议版本</ver>
<dev_type>设备类型</dev_type>
<dev_sub_type>设备子类型</dev_sub_type>
<dev_sn>设备编号</dev_sn>
<cmd >命令字</cmd >
<id>消息id</id>
<rid>反馈消息id</rid>
</head>
<body>
<city>上海</city>
<weathers>
<list>
<value>
<date>2014-2-23</date>
<min_temp>12</min_temp>
<max_temp>25</max_temp>
<pm25>200</pm25>
<type>晴转多云</type>
<desc>天气状况描述</desc>
</value>
</list>
</weathers>
</body>
</data>
Serializer serializer = new Persister();
File source = new File("/data/shareData/example.xml");
try {
CommonUtil.weather1022= serializer.read(Weather1022.class, source);
Log.i("ee",""+CommonUtil.weather1022.getHead().getCmd());
Log.i("ee",""+CommonUtil.weather1022.getWeather1022Body().getCity());
Log.i("ee",""+CommonUtil.weather1022.getWeather1022Body().getWeathersListOfBody().getListweatherValue().get(0).getDesc());
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i("ee","mistake"+e.getMessage());
Toast.makeText(PageActivity.this, "接收数据异常", Toast.LENGTH_SHORT).show();
}
}
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name="value")
public class WeatherValue {
@Element(required = false)
private String date;
@Element(required = false)
private String min_temp;
@Element(required = false)
private String max_temp;
@Element(required = false)
private String pm25;
@Element(required = false)
private String type;
@Element(required = false)
private String desc;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getMin_temp() {
return min_temp;
}
public void setMin_temp(String min_temp) {
this.min_temp = min_temp;
}
public String getMax_temp() {
return max_temp;
}
public void setMax_temp(String max_temp) {
this.max_temp = max_temp;
}
public String getPm25() {
return pm25;
}
public void setPm25(String pm25) {
this.pm25 = pm25;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
@Root(name="weathers",strict=false)
public class WeathersListOfBody {
@ElementList(name="list",required = false)
private List<WeatherValue> listweatherValue;
public List<WeatherValue> getListweatherValue() {
return listweatherValue;
}
public void setListweatherValue(List<WeatherValue> listweatherValue) {
this.listweatherValue = listweatherValue;
}
}
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name="body",strict=false)
public class Weather1022Body {
@Element(required = false)
private String city;
@Element(name="weathers",required = false)
private WeathersListOfBody weathersListOfBody;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public WeathersListOfBody getWeathersListOfBody() {
return weathersListOfBody;
}
public void setWeathersListOfBody(WeathersListOfBody weathersListOfBody) {
this.weathersListOfBody = weathersListOfBody;
}
}
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name="data",strict=false)
public class Weather1022 {
@Element
Head head;
@Element(name="body",required = false)
Weather1022Body weather1022Body;
public Head getHead() {
return head;
}
public void setHead(Head head) {
this.head = head;
}
public Weather1022Body getWeather1022Body() {
return weather1022Body;
}
public void setWeather1022Body(Weather1022Body weather1022Body) {
this.weather1022Body = weather1022Body;
}
}
因为这里我们的xml 有固定的head 变化的body,所以建的类名字有些奇怪,但是不会影响如何解析一个list型的xml