Castor是一个开源的XML数据绑定java组件工具,在XML数据交换方面很有用。
刚开始研究,找了些文档看看:
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
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
下面写个简单例子,测试下Castor:
bean
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;
}
}
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;
}
}
test类:
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);
}
}
}
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);
}
}
}