XStream

下载地址: http://xstream.codehaus.org/download.html


取别名:
package bean;
import java.util.Date;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("Query")
public class Query {
	private Query(){}
	private static Query query = new Query();
	
	@XStreamAlias("CmdType")
	private String CmdType;
	
	@XStreamAlias("SN")
	private int sn;
	
	@XStreamAlias("DeviceID")
	private String DeviceID;
	
	@XStreamAlias("StartTime")
	private Date StartTime;
	
	@XStreamAlias("EndTime")
	private Date EndTime;
	
	@XStreamAlias("FilePath")
	private String filePath;
	
	@XStreamAlias("Address")
	private String address;
	
	@XStreamAlias("Secrecy")
	private int secrecy;
	@XStreamAlias("Type")
	private String type;
	
	@XStreamAlias("RecorderID")
	private String recorderID;
	
	public static Query getInstance(){
		query.setCmdType("RecordInfo");
		query.setSn(17430);
		query.setStartTime(new Date());
		query.setEndTime(new Date());
		query.setFilePath("64010000002100000001");
		query.setAddress("Address");
		query.setSecrecy(0);
		query.setType("time");
		query.setRecorderID("64010000003000000001");
		return query;
	}
	
	public String getCmdType() {
		return CmdType;
	}

	public void setCmdType(String cmdType) {
		CmdType = cmdType;
	}

	public int getSn() {
		return sn;
	}

	public void setSn(int sn) {
		this.sn = sn;
	}

	public String getDeviceID() {
		return DeviceID;
	}

	public void setDeviceID(String deviceID) {
		DeviceID = deviceID;
	}

	public Date getStartTime() {
		return StartTime;
	}

	public void setStartTime(Date startTime) {
		StartTime = startTime;
	}

	public Date getEndTime() {
		return EndTime;
	}

	public void setEndTime(Date endTime) {
		EndTime = endTime;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getRecorderID() {
		return recorderID;
	}

	public void setRecorderID(String recorderID) {
		this.recorderID = recorderID;
	}

	public int getSecrecy() {
		return secrecy;
	}
	public void setSecrecy(int secrecy) {
		this.secrecy = secrecy;
	}
}



import com.thoughtworks.xstream.XStream;

import bean.Query;


public class VieoFile {
	
	
	private static String xml = "<Query> <CmdType>RecordInfo</CmdType><SN>17430</SN><StartTime>2012-09-12 16:30:26.608 CST</StartTime><EndTime>2012-09-12 16:30:26.608 CST</EndTime><FilePath>64010000002100000001</FilePath><Address>Address</Address> <Secrecy>0</Secrecy><Type>time</Type> <RecorderID>64010000003000000001</RecorderID></Query>";
	
	public static void main(String[] args) {
		
		
	/*	Query q = Query.getInstance();
		XStream stream = new XStream();
		stream.autodetectAnnotations(true);
		System.out.println(stream.toXML(q));
		*/
		XStream stream = new XStream();
                //指定使用别名
		stream.autodetectAnnotations(true);
	        //Query与Query类上的别名一致
                stream.alias("Query", Query.class);
		Query query = (Query)stream.fromXML(xml);
	
		
		System.out.println(query.getCmdType());
	}
	

}

你可能感兴趣的:(xstream)