`

JAVA XStream解析xml list对象

    博客分类:
  • xml
阅读更多
<data>
<resultCode>00</resultCode>
<resultMsg>成功</resultMsg>
<hotelBrandResponseList>
	<hotelBrandResponse>
	<brandId>32</brandId>
	<name>如家快捷酒店</name>
	</hotelBrandResponse>
	<hotelBrandResponse>
	<brandId>53</brandId>
	<name>7天连锁酒店</name>
	</hotelBrandResponse>
</hotelBrandResponseList>
</data>

 

// 使用

package com.anxin.ssk;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.Dom4JDriver;

/**
 * @author: (le.qiao)
 * @e-mail: qiaolevip@gmail.com
 * @myblog: <a href="http://qiaolevip.iteye.com">http://qiaolevip.iteye.com</a>
 * @date: 2014-2-13
 * 
 */
public class TestCls {

	public static void main(String[] args) {
		String xml = "<data><resultCode>00</resultCode><resultMsg>成功</resultMsg><hotelBrandResponseList><hotelBrandResponse><brandId>32</brandId><name>如家快捷酒店</name></hotelBrandResponse><hotelBrandResponse><brandId>53</brandId><name>7天连锁酒店</name></hotelBrandResponse></hotelBrandResponseList></data>";
		// XStream xStream = new XStream(new Dom4JDriver());//速度超级慢,不被推荐
                XStream xstream = new XStream(new Xpp3DomDriver());
		xStream.alias("data", Result.class);
		xStream.alias("hotelBrandResponse", HotelBrandResponse.class);
		Result result = (Result) xStream.fromXML(xml);
		System.out.println(result.getResultCode());
		System.out.println(result.getHotelBrandResponseList().size());
		System.out.println(result.getHotelBrandResponseList().get(1).getBrandId());
		System.out.println(result.getHotelBrandResponseList().get(1).getName());
	}
}

 

// 类文件

package com.anxin.ssk;

import java.util.List;

/**
 * @author: (le.qiao)
 * @e-mail: qiaolevip@gmail.com
 * @myblog: <a href="http://qiaolevip.iteye.com">http://qiaolevip.iteye.com</a>
 * @date: 2014-2-13
 * 
 */
public class Result extends BaseResponse {

	private static final long serialVersionUID = -4601687272216326006L;

	private List<HotelBrandResponse> hotelBrandResponseList;

	private HotelBrandResponse hotelBrandResponse;


	/**
	 * @return the hotelBrandResponseList
	 */
	public List<HotelBrandResponse> getHotelBrandResponseList() {
		return hotelBrandResponseList;
	}

	/**
	 * @param hotelBrandResponseList the hotelBrandResponseList to set
	 */
	public void setHotelBrandResponseList(List<HotelBrandResponse> hotelBrandResponseList) {
		this.hotelBrandResponseList = hotelBrandResponseList;
	}

	/**
	 * @return the hotelBrandResponse
	 */
	public HotelBrandResponse getHotelBrandResponse() {
		return hotelBrandResponse;
	}

	/**
	 * @param hotelBrandResponse the hotelBrandResponse to set
	 */
	public void setHotelBrandResponse(HotelBrandResponse hotelBrandResponse) {
		this.hotelBrandResponse = hotelBrandResponse;
	}
}

 

 

package com.anxin.ssk;

import java.io.Serializable;

public class BaseResponse implements Serializable {

	private static final long serialVersionUID = 1L;

	private String resultCode;

	private String resultMsg;

	/**
	 * @return the resultCode
	 */
	public String getResultCode() {
		return resultCode;
	}

	/**
	 * @param resultCode the resultCode to set
	 */
	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}

	/**
	 * @return the resultMsg
	 */
	public String getResultMsg() {
		return resultMsg;
	}

	/**
	 * @param resultMsg the resultMsg to set
	 */
	public void setResultMsg(String resultMsg) {
		this.resultMsg = resultMsg;
	}

}

 

package com.anxin.ssk;

import java.io.Serializable;

public class HotelBrandResponse implements Serializable {

	private static final long serialVersionUID = 1L;

	private String brandId;

	private String name;

	public String getBrandId() {
		return this.brandId;
	}

	public void setBrandId(String brandId) {
		this.brandId = brandId;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics