首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

运用XStream来映射xml中列表元素到java对象

2012-12-28 
使用XStream来映射xml中列表元素到java对象网上很多讲述使用XStream来映射xml文件到java对象的文章,但是几

使用XStream来映射xml中列表元素到java对象
网上很多讲述使用XStream来映射xml文件到java对象的文章,但是几乎没有一篇文章讲述,如何使用XStream来映射xml中的同级的列表元素到java对象的文章。本文示例中将使用XStream 1.3.1。请去XStream的官方站点上下载相应的包。

在Eclipse中新建一个Java Project,然后引入XStream的包:xstream-1.3.1.jar和xpp3-min-1.1.4c.jar。在这里使用xpp的xml解析技术来解析xml,而不是传统的DOM或者sax来进行xml的解析。

在src下新建sample.xml文件。代码如下:

<?xml version="1.0" encoding="UTF-8"?><person id="1" name="sample"><wife name="wife_name" age="25"></wife><house name="house1" address="address1"></house> <house name="house1" address="address1"></house> <house name="house1" address="address1"></house> <house name="house1" address="address1"></house> </person>


新建House类来映射house元素,House类的源代码如下:

public class House {private String name = "";private String address = "";public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}


新建Wife类来映射wife标签,源代码如下:

public class Wife {private String name = "";private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}


新建Person类来映射person元素,源代码如下:
import java.util.List;public class Person {private int id;private String name;private Wife wife;private List<House> houseList;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Wife getWife() {return wife;}public void setWife(Wife wife) {this.wife = wife;}public List<House> getHouseList() {return houseList;}public void setHouseList(List<House> houseList) {this.houseList = houseList;}}


新建TestMain方法,包含main方法。首先,构建XStream的实例:

XStream xstream = new XStream();


然后,就是配置xml文件和java对象之间的映射,源代码如下:
xstream.alias("person", Person.class);xstream.aliasAttribute(Person.class, "id", "id");xstream.aliasAttribute(Person.class, "name", "name");xstream.alias("wife", Wife.class);xstream.aliasAttribute(Wife.class, "name", "name");xstream.addImplicitCollection(Person.class, "houseList");xstream.alias("house", House.class);xstream.aliasAttribute(House.class,"name", "name");xstream.aliasAttribute(House.class, "address", "address");

注意代码中的这句“xstream.addImplicitCollection(Person.class, "houseList");”,如果不加,xml配置文件的house元素将作为houselist的子元素。将变成
<houseList><house name="house1" address="address1"></house> <house name="house1" address="address1"></house> <house name="house1" address="address1"></house> <house name="house1" address="address1"></house> </houseList>

最后,使用XStream.fromXML来将对应的xml转化成java对象。代码如下:
Person person = (Person)xstream.fromXML(TestMain.class.getClassLoader().getResourceAsStream("sample.xml"));

由于sample.xml文件位于classpath下,所以使用了ClassLoader来加载sample.xml。

测试代码如下:
        System.out.println("[person.name=" + person.getName() + "]");System.out.println("[wife.name=" + person.getWife().getName() + ", wife.age=" + person.getWife().getAge() + "]");System.out.println("[person.houseList.size=" + person.getHouseList().size() + "]");System.out.println("[person.houseList.house1.name=" +                person.getHouseList().get(0).getName() + ",address=" + person.getHouseList().get(0).getAddress() +"]");


测试结果如下:
[person.name=sample]
[wife.name=wife_name, wife.age=0]
[person.houseList.size=4]
person.houseList.house1.name=house1,address=address1]

热点排行