Castor实现XML与Java的互转

Castor是一个开源的XML数据绑定java组件工具,在XML数据交换方面很有用。
http://www.castor.org/
 
 
刚开始研究,找了些文档看看:
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.infoq.com/cn/articles/event-streaming-with-smooks
 
下面写个简单例子,测试下Castor:
 
bean
package ex1;

import java.io.Serializable;
import java.util.*;

public class Foo implements Serializable {

         private String name;
         private Date birthday = new Date();
         private List adds = new ArrayList(0);
         private Map map =     new HashMap(0);

         public Foo() {
        }

         public Foo(String name) {
                 this.name = name;
        }

         public String getName() {
                 return name;
        }

         public void setName(String name) {
                 this.name = name;
        }

         public Date getBirthday() {
                 return birthday;
        }

         public void setBirthday(Date birthday) {
                 this.birthday = birthday;
        }

         public List getAdds() {
                 return adds;
        }

         public void setAdds(List adds) {
                 this.adds = adds;
        }

         public Map getMap() {
                 return map;
        }

         public void setMap(Map map) {
                 this.map = map;
        }
}
 
test类:
package ex1;

import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;

import java.io.FileReader;
import java.io.FileWriter;
import java.util.Map;

public class MarshalTester {

         public static void main(String[] args) {
                testMarshaller();
                testUnMarshaller();
        }

         /**
         * java->XML
         */

         public static void testMarshaller() {
                 try {
                        Foo f = new Foo( "foo");
                        f.getAdds().add( "zhengzhou");
                        f.getAdds().add( "xian");
                        f.getMap().put( "a", "aaa");
                        f.getMap().put( "b", "bbb");
                        FileWriter writer = new FileWriter( "foo.xml");
                        Marshaller marshaller = new Marshaller(writer);
                        marshaller.setEncoding( "GBK");
                        marshaller.marshal(f);
                } catch (Exception e) {
                        e.printStackTrace(System.err);
                }
        }

         /**
         * XML->java
         */

         public static void testUnMarshaller() {
                 try {
                        FileReader reader = new FileReader( "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);
                }
        }
}
 
生成的xml文件如下:
foo.xml
"1.0" encoding= "GBK"?>

        foo
        2010-04-30T18:01:59.375+08:00
        "http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
                 xsi:type="java:org.exolab.castor.mapping.MapItem">
                "java:java.lang.String">a
                "java:java.lang.String">aaa
        

        "http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
                 xsi:type="java:org.exolab.castor.mapping.MapItem">
                "java:java.lang.String">b
                "java:java.lang.String">bbb
        

        "http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
                    xsi:type="java:java.lang.String">zhengzhou
        

        "http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"
                    xsi:type="java:java.lang.String">xian
        

 
运行反解组测试方法:
log4j:WARN No appenders could be found for logger (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
 
红色部分是有效输出!
 
虽然格式还不够美观,但已经感受到了Castor的强大功能!
这个例子不够完美,xml还没格式化,输出了一些类参数信息,显得不够专业。以后再研究研究。