java simplexml 序列化

阅读更多



	协议版本
	设备类型
	设备子类型
	设备编号
	命令字  
     消息id
      反馈消息id


上海
 
 
  
    2014-2-23
    12
    25
    200
    晴转多云
    天气状况描述
  
 


 

 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 listweatherValue;

	public List getListweatherValue() {
		return listweatherValue;
	}

	public void setListweatherValue(List 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

你可能感兴趣的:(java simplexml 序列化)