Ajax将数据封装为XML文件

* 第三方工具:xStream
                        * 核心包:xstream-1.4.4.jar

                        * 必要依赖包:xpp3_min-1.1.4c.jar

public class Province {

	private Integer id;
	private String name;
	private List<City> citys;
	public Province(Integer id,String name,List citys) {
		this.id = id;
		this.name = name;
		this.citys = citys;
	}
	public List<City> getCitys() {
		return citys;
	}
	public void setCitys(List<City> citys) {
		this.citys = citys;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

public class City {

	private Integer id;
	private String name;
	public City(Integer id,String name) {
		this.id = id;
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

public class test {

	public static void main(String[] args) {
		/***********************模拟查询数据库结果集**************************/
//		Province p1 = new Province(1, "吉林省");
//		Province p2 = new Province(2, "辽宁省");
//		Province p3 = new Province(3, "山东省");
//		
//		List<Province> provinces = new ArrayList<Province>();
//		provinces.add(p1);
//		provinces.add(p2);
//		provinces.add(p3);
		
		City city1 = new City(1, "长春市");
		City city2 = new City(2, "吉林市");
		City city3 = new City(3, "松原市");
		City city4 = new City(4, "四平市");
		City city5 = new City(5, "通化市");
		
		List<City> citys =  new ArrayList<City>();
		citys.add(city1);
		citys.add(city2);
		citys.add(city3);
		citys.add(city4);
		citys.add(city5);
		
		Province province = new Province(1, "吉林省", citys);
		
		/***********************模拟查询数据库结果集**************************/
		//1 创建XStream类型的实例化对象
		XStream xStream = new XStream();
		
		//2 起别名
		xStream.alias("province", Province.class);
		xStream.alias("city", City.class);
		
		//3 将javabean的属性作为xml格式的标签属性存在
		xStream.useAttributeFor(Province.class, "id");
		xStream.useAttributeFor(Province.class, "name");
		
		//4 利用toXML()方法将javabean、List、Array和Map集合转换成xml格式
		String xml = xStream.toXML(province);
		
		System.out.println(xml);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值