Castor实现XML与Java的互转

前端之家收集整理的这篇文章主要介绍了Castor实现XML与Java的互转前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Castor是一个开源的XML数据绑定java组件工具,在XML数据交换方面很有用。
下面写个简单例子,测试下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@ }
生成的xml文件如下:
foo.xml
<?xml version= "1.0"encoding= "GBK"?> @H_403_7@ <foo> @H_403_7@ <name>foo</name> @H_403_7@ <birthday>2010-04-30T18:01:59.375+08:00</birthday> @H_403_7@ <map xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"@H_403_7@ xsi:type="java:org.exolab.castor.mapping.MapItem">@H_403_7@ <key xsi:type="java:java.lang.String">a</key>@H_403_7@ <value xsi:type="java:java.lang.String">aaa</value>@H_403_7@ </map>@H_403_7@ <map xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"@H_403_7@ xsi:type="java:org.exolab.castor.mapping.MapItem">@H_403_7@ <key xsi:type="java:java.lang.String">b</key>@H_403_7@ <value xsi:type="java:java.lang.String">bbb</value>@H_403_7@ </map>@H_403_7@ <adds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"@H_403_7@ xsi:type="java:java.lang.String">zhengzhou@H_403_7@ </adds>@H_403_7@ <adds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com"@H_403_7@ xsi:type="java:java.lang.String">xian@H_403_7@ </adds>@H_403_7@ </foo>
运行反解组测试方法
log4j:WARN No appenders could be found forlogger (org.castor.core.util.AbstractProperties). @H_403_7@ log4j:WARN Please initialize the log4j system properly. @H_403_7@ Name: foo@H_403_7@ Birthday: Fri Apr 30 18:01:59 CST 2010@H_403_7@ Add: zhengzhou@H_403_7@ Add: xian@H_403_7@ Map: a--aaa@H_403_7@ Map: b--bbb @H_403_7@ @H_403_7@ Process finished with exit code 0
红色部分是有效输出
虽然格式还不够美观,但已经感受到了Castor的强大功能
这个例子不够完美,xml还没格式化,输出了一些类参数信息,显得不够专业。以后再研究研究。
原文链接:https://www.f2er.com/xml/295701.html

猜你在找的XML相关文章