簡介
之前介紹過Apache Commons Digester套件,可以將以xml格式表示的資料轉換成Java的model物件(請參考該網誌)。若要進行相反的動作,即將Java的model物件轉換成xml格式,可以使用XStream(http://xstream.codehaus.org/)套件。XStream的使用方式如下:
XStream xstream = new
XStream();
xstream.alias("addressBook", AddressBook.class);
xstream.alias("person", Person.class);
xstream.alias("address", Address.class);
xstream.addImplicitCollection(AddressBook.class, "personList"); //
to avoid <personList> tag
xstream.addImplicitCollection(Person.class, "addressList"); //
to avoid <addressList> tag
String xml = xstream.toXML(addressBook1);
logger.info("addressBook.toXML(use
xstream) = " + xml);
執行結果:
[2014/09/01 11:27:54] INFO tw.blogspot.saminjava.demo.xstream.AddressBook:171 - addressBook.toXML(use xstream) = <addressBook>
<personList>
<person>
<id>1</id>
<name>Gonzo</name>
<emailMap>
<entry>
<string>business</string>
<string>gonzo@muppets.com</string>
</entry>
</emailMap>
<addressList>
<address>
<type>home</type>
<street>123 Maine Ave.</street>
<city>Las Vegas</city>
<state>NV</state>
<zip>01234</zip>
<country>USA</country>
</address>
<address>
<type>business</type>
<street>234 Maple Dr.</street>
<city>Los Angeles</city>
<state>CA</state>
<zip>98765</zip>
<country>USA</country>
</address>
</addressList>
</person>
<person>
<id>2</id>
<name>Kermit</name>
<emailMap>
<entry>
<string>home</string>
<string>kermie@acme.com</string>
</entry>
<entry>
<string>business</string>
<string>kermit@muppets.com</string>
</entry>
</emailMap>
<addressList>
<address>
<type>business</type>
<street>987 Brown Rd</street>
<city>Las Cruces</city>
<state>NM</state>
<zip>75321</zip>
<country>USA</country>
</address>
</addressList>
</person>
</personList>
</addressBook>
<personList>
<person>
<id>1</id>
<name>Gonzo</name>
<emailMap>
<entry>
<string>business</string>
<string>gonzo@muppets.com</string>
</entry>
</emailMap>
<addressList>
<address>
<type>home</type>
<street>123 Maine Ave.</street>
<city>Las Vegas</city>
<state>NV</state>
<zip>01234</zip>
<country>USA</country>
</address>
<address>
<type>business</type>
<street>234 Maple Dr.</street>
<city>Los Angeles</city>
<state>CA</state>
<zip>98765</zip>
<country>USA</country>
</address>
</addressList>
</person>
<person>
<id>2</id>
<name>Kermit</name>
<emailMap>
<entry>
<string>home</string>
<string>kermie@acme.com</string>
</entry>
<entry>
<string>business</string>
<string>kermit@muppets.com</string>
</entry>
</emailMap>
<addressList>
<address>
<type>business</type>
<street>987 Brown Rd</street>
<city>Las Cruces</city>
<state>NM</state>
<zip>75321</zip>
<country>USA</country>
</address>
</addressList>
</person>
</personList>
</addressBook>
若是使用XStream產出XML格式,要反轉回Java物件,其使用方式如下:
AddressBook addressBook2 =
(AddressBook)xstream.fromXML(xml);
logger.info("addressBook2.toString(use
xstream) = " +
addressBook2.toString());
執行結果:
[2014/09/01 11:44:03] INFO tw.blogspot.saminjava.demo.xstream.AddressBook:175 - addressBook2.toString(use xstream) = tw.blogspot.saminjava.demo.xstream.AddressBook@11f2ee1[personList=[tw.blogspot.saminjava.demo.xstream.Person@3ecfff[id=1,name=Gonzo,emailMap={business=gonzo@muppets.com},addressList=[tw.blogspot.saminjava.demo.xstream.Address@1c99159[type=home,street=123 Maine Ave.,city=Las Vegas,state=NV,zip=01234,country=USA], tw.blogspot.saminjava.demo.xstream.Address@65a77f[type=business,street=234 Maple Dr.,city=Los Angeles,state=CA,zip=98765,country=USA]]], tw.blogspot.saminjava.demo.xstream.Person@1d7ad1c[id=2,name=Kermit,emailMap={home=kermie@acme.com, business=kermit@muppets.com},addressList=[tw.blogspot.saminjava.demo.xstream.Address@a61164[type=business,street=987 Brown Rd,city=Las Cruces,state=NM,zip=75321,country=USA]]]]]
XStream除了XML格式外,還可以另產出JSON格式,其使用方式如下:
xstream = new
XStream(new JettisonMappedXmlDriver());
xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("addressBook", AddressBook.class);
xstream.alias("person", Person.class);
xstream.alias("address", Address.class);
xstream.addImplicitCollection(AddressBook.class, "personList"); //
to avoid <personList> tag
xstream.addImplicitCollection(Person.class, "addressList"); //
to avoid <addressList> tag
String json = xstream.toXML(addressBook1);
logger.info("addressBook.toJSON(use
xstream) = " + json);
執行結果:
[2014/09/01 11:50:04] INFO tw.blogspot.saminjava.demo.xstream.AddressBook:195 - addressBook.toJSON(use xstream) = {"addressBook":{"person":[{"id":1,"name":"Gonzo","emailMap":[{"entry":{"string":["business","gonzo@muppets.com"]}}],"address":[{"type":"home","street":"123 Maine Ave.","city":"Las Vegas","state":"NV","zip":"01234","country":"USA"},{"type":"business","street":"234 Maple Dr.","city":"Los Angeles","state":"CA","zip":98765,"country":"USA"}]},{"id":2,"name":"Kermit","emailMap":[{"entry":[{"string":["home","kermie@acme.com"]},{"string":["business","kermit@muppets.com"]}]}],"address":{"type":"business","street":"987 Brown Rd","city":"Las Cruces","state":"NM","zip":75321,"country":"USA"}}]}}
沒有留言:
張貼留言