1. 什么作用 * 可以把JavaBean转换为(序列化为)xml
2. XStream的jar包
* 核心JAR包:xstream-1.4.7.jar;
* 必须依赖包:xpp3_min-1.1.4c(XML Pull Parser,一款速度很快的XML解析器);
3. 使用步骤
* XStream xstream = new XStream();
* String xmlStr = xstream.toXML(javabean);
4. 使用细节
* 别名:把类型对应的元素名修改了
> xstream.alias("china", List.class):让List类型生成的元素名为china
> xstream.alias("province", Province.class):让Province类型生成的元素名为province
* 使用为属性:默认类的成员,生成的是元素的子元素!我们希望让类的成员生成元素的属性
> xstream.useAttributeFor(Province.class, "name"):把Province类的名为name成员,生成<province>元素的name属性
* 去除Collection类型的成名:我们只需要Collection的内容,而不希望Collection本身也生成一个元素
> xstream.addImplicitCollection(Province.class, "cities"):让Province类的名为cities(它是List类型的,它的内容还会生成元素)的成名不生成元素
* 去除类的指定成名,让其不生成xml元素
> xstream.omitField(City.class, "description"):在生成的xml中不会出现City类的名为description的对应的元素!
具体实例
[java]
view plain
copy
package cn.itcast.demo1;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.thoughtworks.xstream.XStream;
public class Demo1 {
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(
"沈阳",
"shenYang")); p2.addCity(
new City(
"葫芦岛",
"huLuDao")); List<Province> provinceList =
new ArrayList<Province>(); provinceList.add(p1); provinceList.add(p2);
return provinceList; }
@Test public void fun1() { List<Province> proList = getProinvceList(); XStream xstream =
new XStream(); String s = xstream.toXML(proList); System.out.println(s); }
@Test public void fun2() { List<Province> proList = getProinvceList(); XStream xstream =
new XStream(); xstream.alias(
"china", List.
class); xstream.alias(
"province", Province.
class); xstream.alias(
"city", City.
class); String s = xstream.toXML(proList); System.out.println(s); }
@Test public void fun3() { List<Province> proList = getProinvceList(); XStream xstream =
new XStream(); xstream.alias(
"china", List.
class); xstream.alias(
"province", Province.
class); xstream.alias(
"city", City.
class); xstream.useAttributeFor(Province.
class,
"name"); String s = xstream.toXML(proList); System.out.println(s); }
@Test public void fun4() { List<Province> proList = getProinvceList(); XStream xstream =
new XStream(); xstream.alias(
"china", List.
class); xstream.alias(
"province", Province.
class); xstream.alias(
"city", City.
class); xstream.useAttributeFor(Province.
class,
"name"); xstream.addImplicitCollection(Province.
class,
"cities"); String s = xstream.toXML(proList); System.out.println(s); }
@Test public void fun5() { List<Province> proList = getProinvceList(); XStream xstream =
new XStream(); xstream.alias(
"china", List.
class); xstream.alias(
"province", Province.
class); xstream.alias(
"city", City.
class); xstream.useAttributeFor(Province.
class,
"name"); xstream.addImplicitCollection(Province.
class,
"cities"); xstream.omitField(City.
class,
"description"); String s = xstream.toXML(proList); System.out.println(s); } }
文章转自:http://blog.csdn.net/JOKER_SAMA/article/details/61197689
转载请注明原文地址: https://ju.6miu.com/read-665165.html