用Castor 处理XML文档
fmms
13年前
用Castor 处理XML文档 <br /> ——Castor可以完成Java和XML的相互转换 <br /> <br /> 前面有介绍过json-lib这个框架,在线博文:http://www.cnblogs.com/hoojo/archive/2011/04/21/2023805.html <br /> <br /> 以及Jackson这个框架,在线博文:http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html <br /> <br /> 它们都可以完成Java对象到XML的转换,但是还不是那么的完善。还有XStream对JSON及XML的支持,它可以对JSON或XML的完美转换。在线博文: <br /> <br /> http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html <br /> <br /> 这里将介绍Castor来完成Java对象到xml的相互转换。它是怎么样转换的?和前面不同的是castor可以用一个mapping.xml文件来描述转换后的Java对象的xml基本形态,类似于xStream的annotation,这点还是非常不错的。下面我们就来看看Castor是怎么样完成 Java对象到XML之间的相互转换吧。 <br /> <br /> 一、 准备工作 <br /> <br /> 1、 官方资源 <br /> <br /> 本示例会运用到如下依赖包(jar包): <br /> <br /> <br /> <br /> 资源及jar包下载:http://www.castor.org/download.html <br /> <br /> junit jar下载地址: <br /> <br /> https://github.com/KentBeck/junit/downloads <br /> <br /> 关于官方提供的mapping配置相关示例、文档: <br /> <br /> http://www.castor.org/xml-mapping.html <br /> <br /> ibm提供的castor方面的文档资料: <br /> <br /> http://www.google.com.hk/search?hl=zh-CN&newwindow=1&safe=strict&client=aff-cs-360se&hs=Gon&biw=1349&bih=603&q=castor+site%3Awww.ibm.com%2Fdeveloperworks%2Fcn%2Fxml%2F&aq=f&aqi=&aql=&oq= <br /> <br /> 2、 程序测试运行代码 <br /> <br /> <pre class="brush:java; toolbar: true; auto-links: false;">package com.hoo.test; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.exolab.castor.mapping.Mapping; import org.exolab.castor.mapping.MappingException; import org.exolab.castor.xml.MarshalException; import org.exolab.castor.xml.Marshaller; import org.exolab.castor.xml.Unmarshaller; import org.exolab.castor.xml.ValidationException; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.hoo.entity.Account; import com.hoo.entity.AccountArray; import com.hoo.entity.Birthday; import com.hoo.entity.ListBean; import com.hoo.entity.MapBean; /** * <b>function:</b>Castor完成Java对象到XML的相互转换 * 依赖jar: castor-1.3.jar * castor-1.3-core.jar * junit-4.8.2.jar * log4j-1.2.16.jar * commons-logging.jar * @author hoojo * @createDate 2011-4-21 下午07:57:26 * @file CastorTest.java * @package com.hoo.test * @project WebHttpUtils * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */ public class CastorTest { private Account bean = null; private Mapping mapping = new Mapping(); private StringWriter writer = null; private StringReader reader = null; @Before public void init() { bean = new Account(); bean.setAddress("北京"); bean.setEmail("email"); bean.setId(1); bean.setName("jack"); Birthday day = new Birthday(); day.setBirthday("2010-11-22"); bean.setBirthday(day); try { /** * 加载mapping.xml,此文件是对需要转换的Java对象的配置描述, * 即:转换后的Java对象的xml内容的转换规则 */ mapping.loadMapping(System.getProperty("user.dir") + "\\src\\mapping.xml"); } catch (IOException e) { e.printStackTrace(); } catch (MappingException e) { e.printStackTrace(); } } @After public void destory() { bean = null; mapping = null; try { if (writer != null) { writer.flush(); writer.close(); } if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } System.gc(); } public void fail(Object o) { System.out.println(o); } public void failRed(Object o) { System.err.println(o); } }</pre> <br /> <br /> Mapping对象可以完成Java对象到XML的编组和解组,它需要先设定一个mapping.xml,通过xml对JavaObject的描述。来完成JavaObject的编组、解组工作。 <br /> <br /> 3、 看看即将被转换的JavaEntity代码 <br /> <br /> Account <br /> <br /> <pre class="brush:java; toolbar: true; auto-links: false;">package com.hoo.entity; public class Account { private int id; private String name; private String email; private String address; private Birthday birthday; //setter、getter @Override public String toString() { return this.id + "#" + this.name + "#" + this.email + "#" + this.address + "#" + this.birthday; } } Birthday package com.hoo.entity; public class Birthday { private String birthday; public Birthday(String birthday) { super(); this.birthday = birthday; } //getter、setter public Birthday() {} @Override public String toString() { return this.birthday; } } AccountArray package com.hoo.entity; public class AccountArray { private Account[] accounts; private int size; public int getSize() { size = accounts.length; return size; } public void setSize(int size) { this.size = size; } public Account[] getAccounts() { return accounts; } public void setAccounts(Account[] accounts) { this.accounts = accounts; } } ListBean package com.hoo.entity; import java.util.List; public class ListBean { private String name; private List list; //setter、getter } MapBean package com.hoo.entity; import java.util.Map; public class MapBean { private Map<String, Object> map; public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } }</pre> <br /> <br /> 二、 编组JavaObject到XML <br /> <br /> 1、 将JavaBean编组,转换成XML <br /> <br /> <pre class="brush:java; toolbar: true; auto-links: false;">/** * <b>function:</b>将Javabean编组,转换成XML * @author hoojo * @createDate 2011-4-22 下午12:08:48 */ @Test public void writeBean2XML() { writer = new StringWriter(); try { //编组 Marshaller.marshal(bean, writer); fail(writer); } catch (MarshalException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }</pre> <br /> <br /> 代码很简单,通过Marshaller的marshal方法来完成Java对象到XML的编组(序列化、转换)工作。 <br /> <br /> 运行后的结果如下: <br /> <br /> <?xml version="1.0" encoding="UTF-8"?> <br /> <account id="1"><address>北京</address><email>email</email><name>jack</name> <br /> <birthday><birthday>2010-11-22</birthday></birthday></account> <br /> <br /> 2、 将List集合转换成XML <br /> <br /> <pre class="brush:java; toolbar: true; auto-links: false;">/** * <b>function:</b>将List转换成xml * @author hoojo * @createDate 2011-4-22 下午12:11:00 */ @Test public void writeList2XML() { writer = new StringWriter(); List<Account> list = new ArrayList<Account>(); list.add(bean); bean = new Account(); bean.setName("tom"); bean.setId(223); list.add(bean); try { Marshaller.marshal(list, writer); fail(writer); } catch (MarshalException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }</pre> <br /> 运行后,结果如下: <br /> <br /> <pre class="brush:xml; toolbar: true; auto-links: false;"><?xml version="1.0" encoding="UTF-8"?> <array-list> <account xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com" id="1" xsi:type="java:com.hoo.entity.Account"> <address>北京</address><email>email< /email><name>jack</name><birthday><birthday>2010-11-22</birthday></birthday></account> <account xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com" id="223" xsi:type="java:com.hoo.entity.Account"> <name>tom</name> </account> </array-list></pre> <br /> <br /> 怎么样,List存放的是2个Account吧。 <br /> <br /> 3、 将Array数组转换成XML <br /> <br /> <pre class="brush:java; toolbar: true; auto-links: false;">/** * <b>function:</b>将Array数组转换成XML * @author hoojo * @createDate 2011-4-22 下午12:11:25 */ @Test public void writeArray2XML() { writer = new StringWriter(); Account[] acc = new Account[2]; acc[0] = bean; bean = new Account(); bean.setName("tom"); bean.setId(223); acc[1] = bean; try { Marshaller.marshal(acc, writer); fail(writer); } catch (MarshalException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }</pre> <br /> <br /> 结果如下: <br /> <br /> <pre class="brush:xml; toolbar: true; auto-links: false;"><?xml version="1.0" encoding="UTF-8"?> <array><account id="1"><address>北京</address><email>email</email& gt;<name>jack</name> <birthday><birthday>2010-11-22</birthday></birthday></account> <account id="223"><name>tom</name></account></array></pre> <br /> <br /> 4、 转换其他Java类型 <br /> <br /> <pre class="brush:java; toolbar: true; auto-links: false;">/** * <b>function:</b>将Java常用类型编组成xml * @author hoojo * @createDate 2011-4-22 下午12:11:44 */ @Test public void writeObject2XML() { writer = new StringWriter(); try { Marshaller.marshal(true, writer); Marshaller.marshal(9527, writer); Marshaller.marshal(2.2f, writer); Marshaller.marshal(1.11d, writer); Marshaller.marshal("lucy", writer); Marshaller.marshal("hello castor".getBytes(), writer); Marshaller.marshal(new char[] { 'a', 'b', 'c' }, writer); Marshaller.marshal(new String[] { "hi", "spring", "castor" }, writer); fail(writer); } catch (MarshalException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }</pre> <br /> <br /> 结果如下: <br /> <br /> <pre class="brush:xml; toolbar: true; auto-links: false;"><?xml version="1.0" encoding="UTF-8"?> <boolean>true</boolean><?xml version="1.0" encoding="UTF-8"?> <integer>9527</integer><?xml version="1.0" encoding="UTF-8"?> <float>2.2</float><?xml version="1.0" encoding="UTF-8"?> <double>1.11</double><?xml version="1.0" encoding="UTF-8"?> <string>lucy</string><?xml version="1.0" encoding="UTF-8"?> <[-b>aGVsbG8gY2FzdG9y</[-b><?xml version="1.0" encoding="UTF-8"?> <array><character>a</character><character>b</character><character>c</character></array><?xml version="1.0" encoding="UTF-8"?> <array><string>hi</string><string>spring</string><string>castor</string></array></pre> <br /> 都是类型为节点名称,值为text。但是这里并没有出现Map,如果转换Map需要mapping进行配置。下面再慢慢道来-.- <br /> <br /> 5、 将xml解组成JavaBean <br /> <br /> <pre class="brush:java; toolbar: true; auto-links: false;">/** * <b>function:</b>将XML内容,解组成JavaBean * @author hoojo * @createDate 2011-4-22 下午12:12:14 */ @Test public void readXML2Bean() { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<account id=\"1\"><address>北京</address>" + "<name>jack</name><email>email</email>" + "<birthday><birthday>2010-11-22</birthday></birthday></account>"; reader = new StringReader(xml); try { //解组 Account account = (Account) Unmarshaller.unmarshal(Account.class, reader); fail(account); } catch (MarshalException e) { e.printStackTrace(); } catch (ValidationException e) { e.printStackTrace(); } }</pre> <br /> <br /> 结果如下: <br /> <br /> 1#jack#email#北京#2010-11-22 <br /> <br /> 其他的类型,如:map、list、array都不能成功解组。因为这些类型里面有很多系统默认的xml描述。但是利用mapping和自定义JavaBean就可以成功编组和解组了。下面看看mapping是怎么玩转这些类型的。 <br /> <br /> 三、 利用mapping配置,编组JavaObject、解组XML <br /> <br /> 最开始的init方法就提供了mapping,让我们对mapping这个配置有了大概的了解。下面我们将详细介绍mapping是个什么: <br /> <br /> 1、 在此之前我们设置过mapping.xml。如果不设置,肯定是不能转换成我们想要的XML的。那么,mapping.xml配置文件是怎么配置Account这个对象的呢? <br /> <br /> mapping.xml配置如下: <br /> <br /> <pre class="brush:xml; toolbar: true; auto-links: false;"><?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" "http://castor.org/mapping.dtd"> <mapping> <class name="com.hoo.entity.Account" auto-complete="true"> <map-to xml="Account"/> <field name="id" type="integer"> <bind-xml name="id" node="attribute" /> </field> <field name="name" type="string"> <bind-xml name="name" node="element" /> </field> <field name="email" type="string"> <bind-xml name="email" node="element" /> </field> <field name="address" type="string"> <bind-xml name="address" node="element" /> </field> <field name="birthday" type="com.hoo.entity.Birthday"> <bind-xml name="生日" node="element" /> </field> </class> <class name="com.hoo.entity.Birthday"> <map-to xml="birthday" /> <field name="birthday" type="string"> <bind-xml name="birthday" node="attribute" /> </field> </class> </mapping></pre> <br /> <br /> 首先,看看这个xml文档的根元素是mapping,在mapping中可以配置class。也就是我们要转换的JavaObject的配置描述了。 <br /> <br /> class元素的name属性就是配置的JavaObject的classpath路径了。 <br /> <br /> 关于class元素的auto-complate属性,如果这个属性的值为ture。那么编组后的xml,castor会自动给没有在mapping配置文件进行配置的属性自动编组(转换)到xml中。如果为false,那么在mapping配置文件中出现的属性将在编组后不现在在编组后的xml中。 <br /> <br /> map-to就是当前class编组后的xml文档的节点元素名称。 <br /> <br /> field就是描述JavaObject中的属性,name是Java对象的属性名称,type是类型。关于配置的type类型也有规定,你可以参考:http://www.castor.org/xml-mapping.html的field配置讲解。 <br /> <br /> 而field还有其他的属性配置,如get-method应该是getter方法、set-method应该是setter的方法、has-mehtod 应该是hashCode方法,有时候我们不一定要提高getter、setter方法,我们需要用自己的方法名称来代替setter、getter。如果当前field配置的是集合类型,那么你需要给field元素配置collection属性。 <br /> <br /> bind-xml就是绑定(编组)成xml后的xml内容的描述,name就是编组后xml的节点元素名称,node有2个值,分别是 attribute、element。attribute是属性,它会在节点元素的属性中显示,例如:<account id=”2”></account> <br /> <br /> 而element则是单独的一个元素,例如:<account><id>2</id></account> <br /> <br /> 就这个样子的。 <br /> <br /> mapping.xml还可以有其他标签,如: <br /> <br /> <include href="other_mapping_file.xml"/> <br /> <br /> 导入外部xml文件,可以分多个配置。 <br /> <br /> 好了,先将这么多的mapping方面的内容。我们还是看看实际运行的示例吧,代码如下: <br /> <br /> /** <br /> * <b>function:</b>将XML内容解组成Java对象 <br /> * @author hoojo <br /> * @createDate 2011-4-22 下午12:13:28 <br /> */ <br /> @Test <br /> public void bean4Mapping2XML() { <br /> writer = new StringWriter(); <br /> try { <br /> //编组 <br /> Marshaller mar = new Marshaller(writer); <br /> mar.setMapping(mapping); <br /> mar.marshal(bean); <br /> fail(writer); <br /> <br /> //解组 <br /> reader = new StringReader(writer.toString()); <br /> Unmarshaller unmar = new Unmarshaller(Account.class); <br /> unmar.setMapping(mapping); <br /> <br /> Account account = (Account) unmar.unmarshal(reader); <br /> fail(account); <br /> } catch (MarshalException e) { <br /> e.printStackTrace(); <br /> } catch (Exception e) { <br /> e.printStackTrace(); <br /> } <br /> } <br /> <br /> 运行后结果如下: <br /> <br /> <?xml version="1.0" encoding="UTF-8"?> <br /> <Account id="1"><name>jack</name><email>email</email>& lt;address>北京</address><生日 birthday="2010-11-22"/></Account> <br /> 1#jack#email#北京#2010-11-22 <br /> <br /> 上面的xml的根节点是Account,这个功劳就来源于mapping配置中的map-to元素,而根节点的id属性是有field和bind-xml 来完成的。当bind-xml的node值为attribute时,就会以属性的方式显示。当node为element时,就会像后面name、 email一样,以元素名称显示。 <br /> <br /> 再看看上面的mapping文件中的Account的配置,有个auto-complate属性,如果把这个属性的值设置成false,会怎么样?那我们赶紧试试。 <br /> <br /> 没有发现上面异样,但是当我们删除下面配置的filed的时候,就发现有变化了。 <br /> <br /> 结果如下: <br /> <br /> <?xml version="1.0" encoding="UTF-8"?> <br /> <Account><name>jack</name><email>email< /email><address>北京</address><生日 birthday="2010-11-22"/></Account> <br /> 0#jack#email#北京#2010-11-22 <br /> <br /> 发现id没有显示在xml中,那么我们再将auto-complate的属性设置true,会有什么惊喜? <br /> <br /> 结果如下: <br /> <br /> <?xml version="1.0" encoding="UTF-8"?> <br /> <Account id="1"><name>jack</name><email>email</email>& lt;address>北京</address><生日 birthday="2010-11-22"/></Account> <br /> 1#jack#email#北京#2010-11-22 <br /> <br /> 发现id又回来了,但是Account的配置中并没有配置id的field。这是为什么,其实auto-comlate在上面已经讲过了。Castor在编组时会自动将int类型的属性,显示在父元素的属性中。并且JavaObject中有的属性没有在mapping配置文件中配置,castor也会自动将其编组在xml中。 <br /> <br /> 下面我们看看map-to配置的用法,map-to的主要属性是name,也就是我们把当前根元素重命名的名称。Map-to还有2个属性可以用,分别是 ns-uri、ns-prefix。看名称就知道它大概的意识,一个是命名空间的uri另一个则是命名空间的前缀。我们给上面mapping加上这两个属性看看。 <br /> <br /> <map-to xml="Account" ns-uri="http://hoojo.cnblogs.com" ns-prefix="castor"/> <br /> <br /> 结果如下: <br /> <br /> <?xml version="1.0" encoding="UTF-8"?> <br /> <castor:Account xmlns:castor="http://hoojo.cnblogs.com" id="1"><castor:name>jack</castor:name><castor:email>email</castor:email> <br /> <castor:address>北京</castor:address><castor:生日 birthday="2010-11-22"/></castor:Account> <br /> 1#jack#email#北京#2010-11-22 <br /> <br /> 发现了什么?节点元素都带上了ns-prefix的值,而根元素则有了xml的ns。 <br /> <br /> 2、 将一段XML格式字符串转换成JavaBean <br /> <br /> @Test <br /> public void readBean4Mapping2XML() { <br /> String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + <br /> "<Account id=\"2241\"><name>jack</name><email>email< /email><address>北京</address><生日 birthday=\"2010-11-22\"/></Account>"; <br /> try { <br /> reader = new StringReader(xml); <br /> Unmarshaller unmar = new Unmarshaller(Account.class); <br /> unmar.setMapping(mapping); <br /> <br /> Account account = (Account) unmar.unmarshal(reader); <br /> fail(account); <br /> } catch (MarshalException e) { <br /> e.printStackTrace(); <br /> } catch (Exception e) { <br /> e.printStackTrace(); <br /> } <br /> } <br /> <br /> 运行后结果如下: <br /> <br /> 2241#jack#email#北京#2010-11-22 <br /> <br /> 3、 将XML内容解组成Java的Array <br /> <br /> /** <br /> * <b>function:</b>将XML内容解组成Java的Array <br /> * @author hoojo <br /> * @createDate 2011-4-22 下午12:14:50 <br /> */ <br /> @Test <br /> public void array4Mapping2XML() { <br /> writer = new StringWriter(); <br /> Account[] acc = new Account[2]; <br /> acc[0] = bean; <br /> bean = new Account(); <br /> bean.setName("tom"); <br /> bean.setId(223); <br /> acc[1] = bean; <br /> AccountArray array = new AccountArray(); <br /> array.setAccounts(acc); <br /> try { <br /> Marshaller mar = new Marshaller(writer); <br /> mar.setMapping(mapping); <br /> mar.marshal(array); <br /> fail(writer); <br /> <br /> reader = new StringReader(writer.toString()); <br /> Unmarshaller unmar = new Unmarshaller(AccountArray.class); <br /> unmar.setMapping(mapping); <br /> array = (AccountArray) unmar.unmarshal(reader); <br /> fail(array.getSize()); <br /> fail(array.getAccounts()[0]); <br /> fail(array.getAccounts()[1]); <br /> } catch (MarshalException e) { <br /> e.printStackTrace(); <br /> } catch (Exception e) { <br /> e.printStackTrace(); <br /> } <br /> } <br /> <br /> AccountArray的mapping配置如下: <br /> <br /> <class name="com.hoo.entity.AccountArray"> <br /> <map-to xml="account-array"/> <br /> <field name="size" type="int" /> <br /> <field name="accounts" collection="array" type="com.hoo.entity.Account"> <br /> <bind-xml name="accounts" auto-naming="deriveByClass"/> <br /> </field> <br /> </class> <br /> <br /> collection表示是数组,auto-maming有2中值,一种是类driverByClass,另一种则是driverByField是属性。 <br /> <br /> 运行后,结果如下: <br /> <br /> <?xml version="1.0" encoding="UTF-8"?> <br /> <account-array><size>2</size><Account id="1"><name>jack</name><email>email</email>& lt;address>北京</address> <br /> <生日 birthday="2010-11-22"/></Account><Account id="223"><name>tom</name></Account></account-array> <br /> 2 <br /> 1#jack#email#北京#2010-11-22 <br /> 223#tom#null#null#null <br /> <br /> 4、 将Map编组、解组成JavaObject <br /> <br /> /** <br /> * <b>function:</b>xml转换成Java的Map <br /> * @author hoojo <br /> * @createDate 2011-4-22 下午12:15:18 <br /> */ <br /> @Test <br /> public void map4Mapping2XML() { <br /> writer = new StringWriter(); <br /> MapBean mapBean = new MapBean(); <br /> Map<String, Object> map = new HashMap<String, Object>(); <br /> map.put("No1", bean); <br /> bean = new Account(); <br /> bean.setName("tom"); <br /> bean.setId(223); <br /> map.put("No2", bean); <br /> mapBean.setMap(map); <br /> <br /> try { <br /> Marshaller mar = new Marshaller(writer); <br /> mar.setMapping(mapping); <br /> mar.marshal(mapBean); <br /> fail(writer); <br /> <br /> reader = new StringReader(writer.toString()); <br /> Unmarshaller unmar = new Unmarshaller(MapBean.class); <br /> unmar.setMapping(mapping); <br /> mapBean = (MapBean) unmar.unmarshal(reader); <br /> fail(mapBean.getMap()); <br /> } catch (MarshalException e) { <br /> e.printStackTrace(); <br /> } catch (Exception e) { <br /> e.printStackTrace(); <br /> } <br /> } <br /> <br /> Mapping配置 <br /> <br /> <class name="com.hoo.entity.MapBean"> <br /> <field name="map" collection="map"> <br /> <bind-xml name="map"> <br /> <class name="org.exolab.castor.mapping.MapItem"> <br /> <field name="key" type="java.lang.String"> <br /> <bind-xml name="key" node="attribute" /> <br /> </field> <br /> <field name="value" type="com.hoo.entity.Account"> <br /> <bind-xml name="value" auto-naming="deriveByClass"/> <br /> </field> <br /> </class> <br /> </bind-xml> <br /> </field> <br /> </class> <br /> <br /> 上面的map配置必须这样配置,利用org.exolab.castor.mapping.MapItem这个class,完成key、value的配置。 <br /> <br /> 结果如下: <br /> <br /> <?xml version="1.0" encoding="UTF-8"?> <br /> <map-bean><map key="No2"><Account id="223"><name>tom</name></Account></map> <br /> <map key="No1"><Account id="1"><name>jack</name><email>email</email>& lt;address>北京</address> <br /> <生日 birthday="2010-11-22"/></Account></map></map-bean> <br /> {No2=223#tom#null#null#null, No1=1#jack#email#北京#2010-11-22} <br /> <br /> 5、 JavaList编组、解组XML <br /> <br /> /** <br /> * <b>function:</b>List到XML的相互转换 <br /> * @author hoojo <br /> * @createDate 2011-4-22 下午12:16:04 <br /> */ <br /> @SuppressWarnings("unchecked") <br /> @Test <br /> public void listForMapping2XML() { <br /> writer = new StringWriter(); <br /> <br /> List<Account> list = new ArrayList<Account>(); <br /> list.add(bean); <br /> bean = new Account(); <br /> bean.setName("tom"); <br /> bean.setId(223); <br /> list.add(bean); <br /> <br /> ListBean listBean = new ListBean(); <br /> listBean.setList(list); <br /> try { <br /> Marshaller mar = new Marshaller(writer); <br /> mar.setMapping(mapping); <br /> mar.marshal(listBean); <br /> fail(writer); <br /> <br /> reader = new StringReader(writer.toString()); <br /> Unmarshaller unmar = new Unmarshaller(ListBean.class); <br /> unmar.setMapping(mapping); <br /> listBean = (ListBean) unmar.unmarshal(reader); <br /> fail(listBean.getList().size()); <br /> for (Account acc : (List<Account>)listBean.getList()) { <br /> fail(acc); <br /> } <br /> } catch (MarshalException e) { <br /> e.printStackTrace(); <br /> } catch (Exception e) { <br /> e.printStackTrace(); <br /> } <br /> } <br /> <br /> Mapping配置 <br /> <br /> <class name="com.hoo.entity.ListBean"> <br /> <map-to xml="listBean"/> <br /> <field name="list" collection="arraylist" type="com.hoo.entity.Account"> <br /> <bind-xml name="beans" auto-naming="deriveByClass"/> <br /> </field> <br /> </class> <br /> <br /> 结果: <br /> <br /> <?xml version="1.0" encoding="UTF-8"?> <br /> <listBean><Account id="1"><name>jack</name><email>email</email>& lt;address>北京</address> <br /> <生日 birthday="2010-11-22"/></Account><Account id="223"><name>tom</name></Account></listBean> <br /> 2 <br /> 1#jack#email#北京#2010-11-22 <br /> 223#tom#null#null#null <br /> <br /> 原文地址: <a href="/misc/goto?guid=4959500781943199544" target="_blank">http://www.cnblogs.com/hoojo/archive/2011/04/25/2026819.html</a>