XStream的使用

一:功能

  可以将JavaBean转换(序列化)成XMl

二:依赖jar包

  xstream.jar

  xpp3_min.jar(xml pull parser)xml解析器

三:使用步骤

  XStream xstream = new XStream();

  String xmlStream = xstream.toXML(JavaBean);

四:使用细节

  别名:把对应的元素名改了,因为生成的元素名会包含整个JavaBean的全路径

//        指定别名
xStream.alias("china",List.class);
xStream.alias("province",Province.class);
xStream.alias("city",City.class);

  使用为属性,默认类的成员,生成的是子元素,我们希望将它改为属性,如name

//        把Province类型的name属性,生成<province>元素的属性
xStream.useAttributeFor(Province.class,"name");

  去除collection类型的成员名,只是需要collection的内容,不希望collection本身再生成一个子节点去包含它的内容

        xStream.addImplicitCollection(Province.class,"citys");

  去除类的指定的成员名,JavaBean可能包含我们不需要在XML中使用的属性,所以需要去除多有的成员

  xStream.omitField(City.class,"description");

五:源码

JavaBean

package xstream;

/**
* Created by YuWenHui on 2017/4/24 0024.
*/
public class City {
private String name;
private String description; public City(String name, String description) {
this.name = name;
this.description = description;
} public City() {
super();
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
package xstream;

import java.util.ArrayList;
import java.util.List; /**
* Created by YuWenHui on 2017/4/24 0024.
*/
public class Province {
private String name;
List<City> citys = new ArrayList<City>(); public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public List<City> getCitys() {
return citys;
} public void setCitys(List<City> citys) {
this.citys = citys;
} public void addCity(City city){
citys.add(city);
}
}

dome

package xstream;

import com.thoughtworks.xstream.XStream;

import java.util.ArrayList;
import java.util.List; /**
* Created by YuWenHui on 2017/4/24 0024.
*/
public class Dome {
// 返回javabean集合
public List<Province> getProinvceList() {
Province p1 = new Province();
p1.setName("北京");
p1.addCity(new City("东城区", "DongChengQu"));
p1.addCity(new City("昌平区", "ChangPingQu")); Province p2 = new Province();
p2.setName("江西");
p2.addCity(new City("上饶", "ShangRao"));
p2.addCity(new City("宜春", "YiChun")); List<Province> provinceList = new ArrayList<Province>();
provinceList.add(p1);
provinceList.add(p2); return provinceList;
}
@org.junit.Test
public void test(){
List<Province> provinceList = getProinvceList();
XStream xStream = new XStream();
// 指定别名
xStream.alias("china",List.class);
xStream.alias("province",Province.class);
xStream.alias("city",City.class);
// 把Province类型的name属性,生成<province>元素的属性
xStream.useAttributeFor(Province.class,"name");
// 去除collection类型的成员名,只是需要collection的内容,不希望collection本身再生成一个子节点去包含它的内容
xStream.addImplicitCollection(Province.class,"citys");
// 去除类的指定的成员名,JavaBean可能包含我们不需要在XML中使用的属性,所以需要去除多有的成员
xStream.omitField(City.class,"description");
String s = xStream.toXML(provinceList);
System.out.println(s);
}
}

六:结果

<china>
<province name="北京">
<city>
<name>东城区</name>
</city>
<city>
<name>昌平区</name>
</city>
</province>
<province name="江西">
<city>
<name>上饶</name>
</city>
<city>
<name>宜春</name>
</city>
</province>
</china>

上一篇:ZOJ 3785 What day is that day?(今天是星期几?)


下一篇:opengl中VAO,VBO,IBO用法小结(zz) 【转】