——Castor可以完成Java和XML的相互转换
前面有介绍过json-lib这个框架,在线博文:http://www.cnblogs.com/hoojo/archive/2011/04/21/2023805.html
以及Jackson这个框架,在线博文:http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html
它们都可以完成Java对象到XML的转换,但是还不是那么的完善。还有XStream对JSON及XML的支持,它可以对JSON或XML的完美转换。在线博文:
http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html
这 里将介绍Castor来完成Java对象到xml的相互转换。它是怎么样转换的?和前面不同的是castor可以用一个mapping.xml文件来描述 转换后的Java对象的xml基本形态,类似于xStream的annotation,这点还是非常不错的。下面我们就来看看Castor是怎么样完成 Java对象到XML之间的相互转换吧。
一、 准备工作
1、 官方资源
本示例会运用到如下依赖包(jar包):
资源及jar包下载:http://www.castor.org/download.html
junit jar下载地址:
https://github.com/KentBeck/junit/downloads
关于官方提供的mapping配置相关示例、文档:
http://www.castor.org/xml-mapping.html
ibm提供的castor方面的文档资料:
2、 程序测试运行代码
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 [email protected]
* @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);
}
}
Mapping对象可以完成Java对象到XML的编组和解组,它需要先设定一个mapping.xml,通过xml对JavaObject的描述。来完成JavaObject的编组、解组工作。
3、 看看即将被转换的JavaEntity代码
Account
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;
}
}
二、 编组 JavaObject 到 XML
1、 将JavaBean编组,转换成XML
/**
* <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();
}
}
代码很简单,通过Marshaller的marshal方法来完成Java对象到XML的编组(序列化、转换)工作。
运行后的结果如下:
<?xml version="1.0" encoding="UTF-8" ?>
<account id="1"
><address>北京</address><email>email</email><name>jack</name>
<birthday><birthday>2010-11-22</birthday></birthday></account>
2、 将List集合转换成XML
/**
* <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();
}
}
运行后,结果如下:
<?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>
怎么样,List存放的是2个Account吧。
3、 将Array数组转换成XML
/**
* <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();
}
}
结果如下:
<?xml version="1.0" encoding="UTF-8" ?>
<array><account id="1"
><address>北京</address><email>email</email><name>jack</name>
<birthday><birthday>2010-11-22</birthday></birthday></account>
<account id="223"
><name>tom</name></account></array>
4、 转换其他Java类型
/**
* <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();
}
}
结果如下:
<?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>
都是类型为节点名称,值为text。但是这里并没有出现Map,如果转换Map需要mapping进行配置。下面再慢慢道来-.-
5、 将xml解组成JavaBean
/**
* <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();
}
}
结果如下:
1#jack#email#北京#2010-11-22
其他的类型,如:map、list、array都不能成功解组。因为这些类型里面有很多系统默认的xml描述。但是利用mapping和自定义JavaBean就可以成功编组和解组了。下面看看mapping是怎么玩转这些类型的。
三、 利用 mapping 配置,编组 JavaObject 、解组 XML
最开始的init方法就提供了mapping,让我们对mapping这个配置有了大概的了解。下面我们将详细介绍mapping是个什么:
1、 在此之前我们设置过mapping.xml。如果不设置,肯定是不能转换成我们想要的XML的。那么,mapping.xml配置文件是怎么配置Account这个对象的呢?
mapping.xml配置如下:
<? 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 >
首先,看看这个xml文档的根元素是mapping,在mapping中可以配置class。也就是我们要转换的JavaObject的配置描述了。
class元素的name属性就是配置的JavaObject的classpath路径了。
关于class元素的auto-complate属性,如果这个属性的值为ture。那么编组后的xml,castor会自动给没有在 mapping配置文件进行配置的属性自动编组(转换)到xml中。如果为false,那么在mapping配置文件中出现的属性将在编组后不现在在编组 后的xml中。
map-to就是当前class编组后的xml文档的节点元素名称。
field就是描述JavaObject中的属性,name是Java对象的属性名称,type是类型。关于配置的type类型也有规定,你可以参考:http://www.castor.org/xml-mapping.html的field 配置讲解。
而field还有其他的属性配置,如get-method应该是getter方法、set-method应该是setter的方法、 has-mehtod应该是hashCode方法,有时候我们不一定要提高getter、setter方法,我们需要用自己的方法名称来代替 setter、getter。如果当前field配置的是集合类型,那么你需要给field元素配置collection属性。
bind-xml就是绑定(编组)成xml后的xml内容的描述,name就是编组后xml的节点元素名称,node有2个值,分别是 attribute、element。attribute是属性,它会在节点元素的属性中显示,例如:<account id=”2”></account>
而element则是单独的一个元素,例如:<account><id>2</id></account>
就这个样子的。
mapping.xml还可以有其他标签,如:
<include href="other_mapping_file.xml"/>
导入外部xml文件,可以分多个配置。
好了,先将这么多的mapping方面的内容。我们还是看看实际运行的示例吧,代码如下:
/**
* <b>function:</b>将XML内容解组成Java对象
* @author hoojo
* @createDate 2011-4-22 下午12:13:28
*/
@Test
public void bean4Mapping2XML() {
writer = new
StringWriter();
try
{
//编组
Marshaller mar = new
Marshaller(writer);
mar.setMapping(mapping);
mar.marshal(bean);
fail(writer);
//解组
reader = new
StringReader(writer.toString());
Unmarshaller unmar = new Unmarshaller(Account.class );
unmar.setMapping(mapping);
Account account = (Account) unmar.unmarshal(reader);
fail(account);
} catch
(MarshalException e) {
e.printStackTrace();
} catch
(Exception e) {
e.printStackTrace();
}
}
运行后结果如下:
<?xml version="1.0" encoding="UTF-8" ?>
<Account id="1" ><name>jack</name><email>email</email><address>北京</address><生日 birthday="2010-11-22" /></Account>
1#jack#email#北京#2010-11-22
上面的xml的根节点是Account,这个功劳就来源于mapping配置中的map-to元素,而根节点的id属性是有field和bind- xml来完成的。当bind-xml的node值为attribute时,就会以属性的方式显示。当node为element时,就会像后面name、 email一样,以元素名称显示。
再看看上面的mapping文件中的Account的配置,有个auto-complate属性,如果把这个属性的值设置成false,会怎么样?那我们赶紧试试。
没有发现上面异样,但是当我们删除下面配置的filed的时候,就发现有变化了。
结果如下:
<?xml version="1.0" encoding="UTF-8" ?>
<Account><name>jack</name><email>email</email><address>北京</address><生日 birthday="2010-11-22"
/></Account>
0#jack#email#北京#2010-11-22
发现id没有显示在xml中,那么我们再将auto-complate的属性设置true,会有什么惊喜?
结果如下:
<?xml version="1.0" encoding="UTF-8" ?>
<Account id="1" ><name>jack</name><email>email</email><address>北京</address><生日 birthday="2010-11-22" /></Account>
1#jack#email#北京#2010-11-22
发现id又回来了,但是Account的配置中并没有配置id的field。这是为什么,其实auto-comlate在上面已经讲过了。 Castor在编组时会自动将int类型的属性,显示在父元素的属性中。并且JavaObject中有的属性没有在mapping配置文件中配 置,castor也会自动将其编组在xml中。
下面我们看看map-to配置的用法,map-to的主要属性是name,也就是我们把当前根元素重命名的名称。Map-to还有2个属性 可以用,分别是ns-uri、ns-prefix。看名称就知道它大概的意识,一个是命名空间的uri另一个则是命名空间的前缀。我们给上面 mapping加上这两个属性看看。
<map-to xml="Account" ns-uri="http://hoojo.cnblogs.com" ns-prefix="castor"/>
结果如下:
<?xml version="1.0" encoding="UTF-8" ?>
<castor:Account xmlns:castor="http://hoojo.cnblogs.com" id="1" ><castor:name>jack</castor:name><castor:email>email</castor:email>
<castor:address>北京</castor:address><castor:生日 birthday="2010-11-22"
/></castor:Account>
1#jack#email#北京#2010-11-22
发现了什么?节点元素都带上了ns-prefix的值,而根元素则有了xml的ns。
2、 将一段XML格式字符串转换成JavaBean
@Test
public void readBean4Mapping2XML() {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+
"<Account id=\"2241\"><name>jack</name><email>email</email><address>北京</address><生日 birthday=\"2010-11-22\"/></Account>"
;
try
{
reader = new
StringReader(xml);
Unmarshaller unmar = new Unmarshaller(Account.class );
unmar.setMapping(mapping);
Account account = (Account) unmar.unmarshal(reader);
fail(account);
} catch
(MarshalException e) {
e.printStackTrace();
} catch
(Exception e) {
e.printStackTrace();
}
}
运行后结果如下:
2241#jack#email#北京#2010-11-22
3、 将XML内容解组成Java的Array
/**
* <b>function:</b>将XML内容解组成Java的Array
* @author hoojo
* @createDate 2011-4-22 下午12:14:50
*/
@Test
public void array4Mapping2XML() {
writer = new
StringWriter();
Account[] acc = new
Account[2];
acc[0] = bean;
bean = new
Account();
bean.setName("tom"
);
bean.setId(223);
acc[1] = bean;
AccountArray array = new
AccountArray();
array.setAccounts(acc);
try
{
Marshaller mar = new
Marshaller(writer);
mar.setMapping(mapping);
mar.marshal(array);
fail(writer);
reader = new
StringReader(writer.toString());
Unmarshaller unmar = new Unmarshaller(AccountArray.class );
unmar.setMapping(mapping);
array = (AccountArray) unmar.unmarshal(reader);
fail(array.getSize());
fail(array.getAccounts()[0]);
fail(array.getAccounts()[1]);
} catch
(MarshalException e) {
e.printStackTrace();
} catch
(Exception e) {
e.printStackTrace();
}
}
AccountArray的mapping配置如下:
< class name ="com.hoo.entity.AccountArray" >
< map-to xml ="account-array" />
< field name ="size" type ="int" />
< field name ="accounts" collection ="array" type ="com.hoo.entity.Account" >
< bind-xml name ="accounts" auto-naming ="deriveByClass" />
</ field >
</ class >
collection表示是数组,auto-maming有2中值,一种是类driverByClass,另一种则是driverByField是属性。
运行后,结果如下:
<?xml version="1.0" encoding="UTF-8" ?>
<account-array><size>2</size><Account id="1"
><name>jack</name><email>email</email><address>北京</address>
<生日 birthday="2010-11-22" /></Account><Account id="223" ><name>tom</name></Account></account-array>
2
1#jack#email#北京#2010-11-22
223#tom#null#null#null
4、 将Map编组、解组成JavaObject
/**
* <b>function:</b>xml转换成Java的Map
* @author hoojo
* @createDate 2011-4-22 下午12:15:18
*/
@Test
public void map4Mapping2XML() {
writer = new
StringWriter();
MapBean mapBean = new
MapBean();
Map<String, Object> map = new
HashMap<String, Object>();
map.put("No1"
, bean);
bean = new
Account();
bean.setName("tom"
);
bean.setId(223);
map.put("No2"
, bean);
mapBean.setMap(map);
try
{
Marshaller mar = new
Marshaller(writer);
mar.setMapping(mapping);
mar.marshal(mapBean);
fail(writer);
reader = new
StringReader(writer.toString());
Unmarshaller unmar = new Unmarshaller(MapBean.class );
unmar.setMapping(mapping);
mapBean = (MapBean) unmar.unmarshal(reader);
fail(mapBean.getMap());
} catch
(MarshalException e) {
e.printStackTrace();
} catch
(Exception e) {
e.printStackTrace();
}
}
Mapping配置
< class name ="com.hoo.entity.MapBean" >
< field name ="map" collection ="map" >
< bind-xml name ="map" >
< class name ="org.exolab.castor.mapping.MapItem" >
< field name ="key" type ="java.lang.String" >
< bind-xml name ="key" node ="attribute" />
</ field >
< field name ="value" type ="com.hoo.entity.Account" >
< bind-xml name ="value" auto-naming ="deriveByClass" />
</ field >
</ class >
</ bind-xml >
</ field >
</ class >
上面的map配置必须这样配置,利用org.exolab.castor.mapping.MapItem 这个class,完成key、value的配置。
结果如下:
<?xml version="1.0" encoding="UTF-8" ?>
<map-bean><map key="No2" ><Account id="223" ><name>tom</name></Account></map>
<map key="No1" ><Account id="1" ><name>jack</name><email>email</email><address>北京</address>
<生日 birthday="2010-11-22"
/></Account></map></map-bean>
{No2=223#tom#null#null#null, No1=1#jack#email#北京#2010-11-22}
5、 JavaList编组、解组XML
/**
* <b>function:</b>List到XML的相互转换
* @author hoojo
* @createDate 2011-4-22 下午12:16:04
*/
@SuppressWarnings("unchecked"
)
@Test
public void listForMapping2XML() {
writer = new
StringWriter();
List<Account> list = new
ArrayList<Account>();
list.add(bean);
bean = new
Account();
bean.setName("tom"
);
bean.setId(223);
list.add(bean);
ListBean listBean = new
ListBean();
listBean.setList(list);
try
{
Marshaller mar = new
Marshaller(writer);
mar.setMapping(mapping);
mar.marshal(listBean);
fail(writer);
reader = new
StringReader(writer.toString());
Unmarshaller unmar = new Unmarshaller(ListBean.class );
unmar.setMapping(mapping);
listBean = (ListBean) unmar.unmarshal(reader);
fail(listBean.getList().size());
for
(Account acc : (List<Account>)listBean.getList()) {
fail(acc);
}
} catch
(MarshalException e) {
e.printStackTrace();
} catch
(Exception e) {
e.printStackTrace();
}
}
Mapping配置
< class name ="com.hoo.entity.ListBean" >
< map-to xml ="listBean" />
< field name ="list" collection ="arraylist" type ="com.hoo.entity.Account" >
< bind-xml name ="beans" auto-naming ="deriveByClass" />
</ field >
</ class >
结果:
<?xml version="1.0" encoding="UTF-8" ?>
<listBean><Account id="1"
><name>jack</name><email>email</email><address>北京</address>
<生日 birthday="2010-11-22" /></Account><Account id="223" ><name>tom</name></Account></listBean>
2
1#jack#email#北京#2010-11-22
223#tom#null#null#null