使用 Xstream 可以帮助我们完成繁琐的 xml 和 JavaBean 之间的转换。
gradle 依赖:
compile 'com.thoughtworks.xstream:xstream:1.4.8'
compile 'dom4j:dom4j:1.6.1'
首先来看一个非常简单的例子:
例1:
@Test
public void test01(){
// 1、创建 XStream 对象
XStream xStream = new XStream();
// 2、设置别名(默认使用的是包名,可以使用 alias 将包名改写成指定的名字)
xStream.alias("person",Person.class);
// 3、创建对象
Person p = new Person("liwei","123456",28,14.19);
// 4、转换成 xml
String xml = xStream.toXML(p);
System.out.println(xml);
}
输出结果:
<person>
<username>liwei</username>
<password>123456</password>
<age>28</age>
<price>14.19</price>
</person>
例2:
/** * 更改其中的属性名称 */
@Test
public void test02(){
// 1、创建 XStream 对象
XStream xStream = new XStream();
// 2、设置别名(默认使用的是包名,可以使用 alias 将包名改写成指定的名字)
xStream.alias("person",Person.class);
// 改属性名,类的属性名是 username,生成的 xml 子节点名字改成 nickname(一般不用)
xStream.aliasField("nickname",Person.class,"username");
// 3、创建对象
Person p = new Person("liwei","123456",28,14.19);
// 4、转换成 xml
String xml = xStream.toXML(p);
System.out.println(xml);
}
输出结果:
<person>
<nickname>liwei</nickname>
<password>123456</password>
<age>28</age>
<price>14.19</price>
</person>
例3:
/** * 把某个子节点改成属性名 * 把 password 属性输出的时候从 xml 子节点变成 xml 属性 */
@Test
public void test03(){
// 1、创建 XStream 对象
XStream xStream = new XStream();
// 2、设置别名(默认使用的是包名,可以使用 alias 将包名改写成指定的名字)
xStream.alias("person",Person.class);
// 改属性名,类的属性名是 username,生成的 xml 子节点名字改成 nickname(一般不用)
xStream.aliasField("nickname",Person.class,"username");
xStream.useAttributeFor(Person.class,"password");
// 3、创建对象
Person p = new Person("liwei","123456",28,14.19);
// 4、转换成 xml
String xml = xStream.toXML(p);
System.out.println(xml);
}
输出结果:
<person password="123456">
<nickname>liwei</nickname>
<age>28</age>
<price>14.19</price>
</person>
例4:
@Test
public void test04(){
XStream xStream = new XStream();
xStream.alias("person",Person.class);
xStream.alias("persons",List.class);
List<Person> personList = new ArrayList<>();
Person p1 = new Person("liwei","123456",28,14.19,new Group(1,"研发部"));
Person p2 = new Person("zhouguang","666666",28,25.26,new Group(2,"交通大学"));
Person p3 = new Person("huzhenyu","88888",23,16.18,new Group(3,"系统工程"));
Person p4 = new Person("wudi","9999",25,18.14,new Group(4,"自营公司"));
personList.add(p1);
personList.add(p2);
personList.add(p3);
personList.add(p4);
String xml = xStream.toXML(personList);
System.out.println(xml);
}
例5:
/** * 须要牢牢记住一点,xml 一定要有根节点,并且只能有一个根节点 */
@Test
public void test05(){
XStream xStream = new XStream();
xStream.alias("person",Person.class);
xStream.alias("persons",List.class);
// xStream.addImplicitCollection(Person.class,"List");
List<Person> personList = new ArrayList<>();
Person p1 = new Person("liwei","123456",28,14.19,new Group(1,"研发部"));
Person p2 = new Person("zhouguang","666666",28,25.26,new Group(2,"交通大学"));
Person p3 = new Person("huzhenyu","88888",23,16.18,new Group(3,"系统工程"));
Person p4 = new Person("wudi","9999",25,18.14,new Group(4,"自营公司"));
personList.add(p1);
personList.add(p2);
personList.add(p3);
personList.add(p4);
String xml = xStream.toXML(personList);
System.out.println(xml);
}
例6:
@Test
public void test06(){
XStream xStream = new XStream();
xStream.alias("group",Group.class);
xStream.alias("person",Person.class);
xStream.addImplicitCollection(Group.class,"personList");
Group group = new Group(1,"研发部");
Person p1 = new Person("liwei","123456",28,14.19,new Group(1,"研发部"));
Person p2 = new Person("zhouguang","666666",28,25.26,new Group(2,"交通大学"));
Person p3 = new Person("huzhenyu","88888",23,16.18,new Group(3,"系统工程"));
Person p4 = new Person("wudi","9999",25,18.14,new Group(4,"自营公司"));
group.addPerson(p1);
group.addPerson(p2);
group.addPerson(p3);
group.addPerson(p4);
String xml = xStream.toXML(group);
System.out.println(xml);
}
输出结果:
<group>
<id>1</id>
<name>研发部</name>
<person>
<username>liwei</username>
<password>123456</password>
<age>28</age>
<price>14.19</price>
<group>
<id>1</id>
<name>研发部</name>
</group>
</person>
<person>
<username>zhouguang</username>
<password>666666</password>
<age>28</age>
<price>25.26</price>
<group>
<id>2</id>
<name>交通大学</name>
</group>
</person>
<person>
<username>huzhenyu</username>
<password>88888</password>
<age>23</age>
<price>16.18</price>
<group>
<id>3</id>
<name>系统工程</name>
</group>
</person>
<person>
<username>wudi</username>
<password>9999</password>
<age>25</age>
<price>18.14</price>
<group>
<id>4</id>
<name>自营公司</name>
</group>
</person>
</group>
例7:
/** * 把 XML 文件片段转换成 对象 */
@Test
public void test07(){
XStream xStream = new XStream();
xStream.alias("person",Person.class);
String xml = "<person><username>liwei</username><password>123456</password></person>";
Person person = (Person) xStream.fromXML(xml);
System.out.println(person.getUsername());
System.out.println(person.getPassword());
}
例8:
@Test
public void test08(){
XStream xStream = new XStream();
xStream.alias("persons",List.class);
xStream.alias("person",Person.class);
List<Person> personList =
(List<Person>)xStream.fromXML(XstreamTest.class.getClassLoader().getResourceAsStream("user.xml"));
for(Person person:personList){
System.out.println(person);
}
}
参考资料:
http://www.yiibai.com/xstream/xstream_first_application.html