Castor是一个开源的XML数据绑定java组件工具,在XML数据交换方面很有用。
刚开始研究,找了些文档看看:
@H_403_7@
http://www.ibm.com/developerworks/cn/xml/x-bindcastor/
@H_403_7@
http://www.ibm.com/developerworks/cn/xml/x-xjavacastor1/
@H_403_7@
http://www.ibm.com/developerworks/cn/xml/x-xjavacastor2/
@H_403_7@
http://www.ibm.com/developerworks/cn/xml/x-pracdb1.html
下面写个简单例子,测试下Castor:
bean
packageex1;
@H_403_7@
@H_403_7@
importjava.io.Serializable;
@H_403_7@
importjava.util.*;
@H_403_7@
@H_403_7@
public
classFoo
implementsSerializable {
@H_403_7@
@H_403_7@
privateString name;
@H_403_7@
privateDate birthday =
newDate();
@H_403_7@
privateList adds =
newArrayList(0);
@H_403_7@
privateMap map =
newHashMap(0);
@H_403_7@
@H_403_7@
publicFoo() {
@H_403_7@ }
@H_403_7@
@H_403_7@
publicFoo(String name) {
@H_403_7@
this.name = name;
@H_403_7@ }
@H_403_7@
@H_403_7@
publicString getName() {
@H_403_7@
returnname;
@H_403_7@ }
@H_403_7@
@H_403_7@
public
voidsetName(String name) {
@H_403_7@
this.name = name;
@H_403_7@ }
@H_403_7@
@H_403_7@
publicDate getBirthday() {
@H_403_7@
returnbirthday;
@H_403_7@ }
@H_403_7@
@H_403_7@
public
voidsetBirthday(Date birthday) {
@H_403_7@
this.birthday = birthday;
@H_403_7@ }
@H_403_7@
@H_403_7@
publicList getAdds() {
@H_403_7@
returnadds;
@H_403_7@ }
@H_403_7@
@H_403_7@
public
voidsetAdds(List adds) {
@H_403_7@
this.adds = adds;
@H_403_7@ }
@H_403_7@
@H_403_7@
publicMap getMap() {
@H_403_7@
returnmap;
@H_403_7@ }
@H_403_7@
@H_403_7@
public
voidsetMap(Map map) {
@H_403_7@
this.map = map;
@H_403_7@ }
@H_403_7@ }
test类:
packageex1;
@H_403_7@
@H_403_7@
importorg.exolab.castor.xml.Marshaller;
@H_403_7@
importorg.exolab.castor.xml.Unmarshaller;
@H_403_7@
@H_403_7@
importjava.io.FileReader;
@H_403_7@
importjava.io.FileWriter;
@H_403_7@
importjava.util.Map;
@H_403_7@
@H_403_7@
public
classMarshalTester {
@H_403_7@
@H_403_7@
public
static
voidmain(String[] args) {
@H_403_7@ testMarshaller();
@H_403_7@ testUnMarshaller();
@H_403_7@ }
@H_403_7@
@H_403_7@
/**@H_403_7@ * java->XML@H_403_7@ */
@H_403_7@
public
static
voidtestMarshaller() {
@H_403_7@
try{
@H_403_7@ Foo f =
newFoo(
"foo");
@H_403_7@ f.getAdds().add(
"zhengzhou");
@H_403_7@ f.getAdds().add(
"xian");
@H_403_7@ f.getMap().put(
"a","aaa");
@H_403_7@ f.getMap().put(
"b","bbb");
@H_403_7@ FileWriter writer =
newFileWriter(
"foo.xml");
@H_403_7@ Marshaller marshaller =
newMarshaller(writer);
@H_403_7@ marshaller.setEncoding(
"GBK");
@H_403_7@ marshaller.marshal(f);
@H_403_7@ }
catch(Exception e) {
@H_403_7@ e.printStackTrace(System.err);
@H_403_7@ }
@H_403_7@ }
@H_403_7@
@H_403_7@
/**@H_403_7@ * XML->java@H_403_7@ */
@H_403_7@
public
static
voidtestUnMarshaller() {
@H_403_7@
try{
@H_403_7@ FileReader reader =
newFileReader(
"D:\\teststu\\testcastor\\foo.xml");
@H_403_7@ Foo foo = (Foo) Unmarshaller.unmarshal(Foo.
class,reader);
@H_403_7@ System.out.println(
"Name: "+ foo.getName());
@H_403_7@ System.out.println(
"Birthday: "+ foo.getBirthday());
@H_403_7@
for(Object s : foo.getAdds()) {
@H_403_7@ System.out.println(
"Add: "+ s.toString());
@H_403_7@ }
@H_403_7@
@H_403_7@
for(Object o : foo.getMap().entrySet()) {
@H_403_7@ Map.Entry e = (Map.Entry) o;
@H_403_7@ System.out.println(
"Map: "+ e.getKey() +
"--"+ e.getValue());
@H_403_7@ }
@H_403_7@
@H_403_7@ }
catch(Exception e) {
@H_403_7@ System.err.println(e.getMessage());
@H_403_7@ e.printStackTrace(System.err);
@H_403_7@ }
@H_403_7@ }
@H_403_7@ }