Castor实现XML与Java的互转

前端之家收集整理的这篇文章主要介绍了Castor实现XML与Java的互转前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Castor是一个开源的XML数据绑定java组件工具,在XML数据交换方面很有用。
下面写个简单例子,测试下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;
}
}
test类:
@H_403_150@ 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);
}
}
}
生成的xml文件如下:
foo.xml
@H_403_150@ <?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_403_150@ 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
红色部分是有效输出
虽然格式还不够美观,但已经感受到了Castor的强大功能
这个例子不够完美,xml还没格式化,输出了一些类参数信息,显得不够专业。以后再研究研究。

猜你在找的XML相关文章