Castor是一个开源的XML数据绑定java组件工具,在XML数据交换方面很有用。
@H_502_1@
http://www.castor.org/
@H_502_1@
@H_502_1@
刚开始研究,找了些文档看看:
http://www.ibm.com/developerworks/cn/xml/x-bindcastor/
http://www.ibm.com/developerworks/cn/xml/x-xjavacastor1/
http://www.ibm.com/developerworks/cn/xml/x-xjavacastor2/
http://www.ibm.com/developerworks/cn/xml/x-pracdb1.html @H_502_1@
http://www.ibm.com/developerworks/cn/xml/x-bindcastor/
http://www.ibm.com/developerworks/cn/xml/x-xjavacastor1/
http://www.ibm.com/developerworks/cn/xml/x-xjavacastor2/
http://www.ibm.com/developerworks/cn/xml/x-pracdb1.html @H_502_1@
http://www.infoq.com/cn/articles/event-streaming-with-smooks
@H_502_1@
下面写个简单例子,测试下Castor:
@H_502_1@
bean
@H_502_1@
packageex1;
importjava.io.Serializable;
importjava.util.*;
public classFoo implementsSerializable {
privateString name;
privateDate birthday = newDate();
privateList adds = newArrayList(0);
privateMap map = newHashMap(0);
publicFoo() {
}
publicFoo(String name) {
this.name = name;
}
publicString getName() {
returnname;
}
public voidsetName(String name) {
this.name = name;
}
publicDate getBirthday() {
returnbirthday;
}
public voidsetBirthday(Date birthday) {
this.birthday = birthday;
}
publicList getAdds() {
returnadds;
}
public voidsetAdds(List adds) {
this.adds = adds;
}
publicMap getMap() {
returnmap;
}
public voidsetMap(Map map) {
this.map = map;
}
} @H_502_1@ @H_502_1@ @H_502_1@ @H_502_1@
importjava.io.Serializable;
importjava.util.*;
public classFoo implementsSerializable {
privateString name;
privateDate birthday = newDate();
privateList adds = newArrayList(0);
privateMap map = newHashMap(0);
publicFoo() {
}
publicFoo(String name) {
this.name = name;
}
publicString getName() {
returnname;
}
public voidsetName(String name) {
this.name = name;
}
publicDate getBirthday() {
returnbirthday;
}
public voidsetBirthday(Date birthday) {
this.birthday = birthday;
}
publicList getAdds() {
returnadds;
}
public voidsetAdds(List adds) {
this.adds = adds;
}
publicMap getMap() {
returnmap;
}
public voidsetMap(Map map) {
this.map = map;
}
} @H_502_1@ @H_502_1@ @H_502_1@ @H_502_1@
test类:
@H_502_1@
packageex1;
importorg.exolab.castor.xml.Marshaller;
importorg.exolab.castor.xml.Unmarshaller;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.util.Map;
public classMarshalTester {
public static voidmain(String[] args) {
testMarshaller();
testUnMarshaller();
}
/**
* java->XML
*/
public static voidtestMarshaller() {
try{
Foo f = newFoo( "foo");
f.getAdds().add( "zhengzhou");
f.getAdds().add( "xian");
f.getMap().put( "a","aaa");
f.getMap().put( "b","bbb");
FileWriter writer = newFileWriter( "foo.xml");
Marshaller marshaller = newMarshaller(writer);
marshaller.setEncoding( "GBK");
marshaller.marshal(f);
} catch(Exception e) {
e.printStackTrace(System.err);
}
}
/**
* XML->java
*/
public static voidtestUnMarshaller() {
try{
FileReader reader = newFileReader( "D:\\teststu\\testcastor\\foo.xml");
Foo foo = (Foo) Unmarshaller.unmarshal(Foo. class,reader);
System.out.println( "Name: "+ foo.getName());
System.out.println( "Birthday: "+ foo.getBirthday());
for(Object s : foo.getAdds()) {
System.out.println( "Add: "+ s.toString());
}
for(Object o : foo.getMap().entrySet()) {
Map.Entry e = (Map.Entry) o;
System.out.println( "Map: "+ e.getKey() + "--"+ e.getValue());
}
} catch(Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
}
}
} @H_502_1@ @H_502_1@ @H_502_1@ @H_502_1@
importorg.exolab.castor.xml.Marshaller;
importorg.exolab.castor.xml.Unmarshaller;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.util.Map;
public classMarshalTester {
public static voidmain(String[] args) {
testMarshaller();
testUnMarshaller();
}
/**
* java->XML
*/
public static voidtestMarshaller() {
try{
Foo f = newFoo( "foo");
f.getAdds().add( "zhengzhou");
f.getAdds().add( "xian");
f.getMap().put( "a","aaa");
f.getMap().put( "b","bbb");
FileWriter writer = newFileWriter( "foo.xml");
Marshaller marshaller = newMarshaller(writer);
marshaller.setEncoding( "GBK");
marshaller.marshal(f);
} catch(Exception e) {
e.printStackTrace(System.err);
}
}
/**
* XML->java
*/
public static voidtestUnMarshaller() {
try{
FileReader reader = newFileReader( "D:\\teststu\\testcastor\\foo.xml");
Foo foo = (Foo) Unmarshaller.unmarshal(Foo. class,reader);
System.out.println( "Name: "+ foo.getName());
System.out.println( "Birthday: "+ foo.getBirthday());
for(Object s : foo.getAdds()) {
System.out.println( "Add: "+ s.toString());
}
for(Object o : foo.getMap().entrySet()) {
Map.Entry e = (Map.Entry) o;
System.out.println( "Map: "+ e.getKey() + "--"+ e.getValue());
}
} catch(Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
}
}
} @H_502_1@ @H_502_1@ @H_502_1@ @H_502_1@
生成的xml文件如下:
@H_502_1@
foo.xml
@H_502_1@
<?xml version=
"1.0"encoding=
"GBK"?>
<foo>
<name>foo</name>
<birthday>2010-04-30T18:01:59.375+08:00</birthday>
<map xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
xsi:type="java:org.exolab.castor.mapping.MapItem">
<key xsi:type="java:java.lang.String">a</key>
<value xsi:type="java:java.lang.String">aaa</value>
</map>
<map xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
xsi:type="java:org.exolab.castor.mapping.MapItem">
<key xsi:type="java:java.lang.String">b</key>
<value xsi:type="java:java.lang.String">bbb</value>
</map>
<adds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
xsi:type="java:java.lang.String">zhengzhou
</adds>
<adds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
xsi:type="java:java.lang.String">xian
</adds>
</foo> @H_502_1@ @H_502_1@ @H_502_1@ @H_502_1@
<foo>
<name>foo</name>
<birthday>2010-04-30T18:01:59.375+08:00</birthday>
<map xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
xsi:type="java:org.exolab.castor.mapping.MapItem">
<key xsi:type="java:java.lang.String">a</key>
<value xsi:type="java:java.lang.String">aaa</value>
</map>
<map xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
xsi:type="java:org.exolab.castor.mapping.MapItem">
<key xsi:type="java:java.lang.String">b</key>
<value xsi:type="java:java.lang.String">bbb</value>
</map>
<adds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
xsi:type="java:java.lang.String">zhengzhou
</adds>
<adds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
xsi:type="java:java.lang.String">xian
</adds>
</foo> @H_502_1@ @H_502_1@ @H_502_1@ @H_502_1@
运行反解组测试方法:
@H_502_1@
log4j:WARN No appenders could be found
forlogger (org.castor.core.util.AbstractProperties).
log4j:WARN Please initialize the log4j system properly.
Name: foo
Birthday: Fri Apr 30 18:01:59 CST 2010
Add: zhengzhou
Add: xian
Map: a--aaa
Map: b--bbb
Process finished with exit code 0 @H_502_1@ @H_502_1@ @H_502_1@ @H_502_1@
log4j:WARN Please initialize the log4j system properly.
Name: foo
Birthday: Fri Apr 30 18:01:59 CST 2010
Add: zhengzhou
Add: xian
Map: a--aaa
Map: b--bbb
Process finished with exit code 0 @H_502_1@ @H_502_1@ @H_502_1@ @H_502_1@