【文章內(nèi)容簡介】
try titlefirst/title descriptionMy first blog entry./description /entry entry titletutorial/title descriptionToday we have developed a nice alias tutorial. Tell your friends! NOW!/description /entry/blog 對package進(jìn)行aliasXStream另外一個(gè)功能是對package進(jìn)行alias,雖然這個(gè)功能比較少用。修改main函數(shù),如下: public static void main(String[] args) { Blog teamBlog = new Blog(new Author(Guilherme Silveira))。 (new Entry(first, My first blog entry.))。 (new Entry(tutorial, Today we have developed a nice alias tutorial. Tell your friends! NOW!))。 XStream xstream = new XStream()。 (, )。 ((teamBlog))。 }運(yùn)行以上結(jié)果,輸出如下: writer nameGuilherme Silveira/name /writer entries titlefirst/title descriptionMy first blog entry./description / titletutorial/title descriptionToday we have developed a nice alias tutorial. Tell your friends! NOW!/description / /entries/5 Converter機(jī)制XStream提供了Converter,使我們在objectxml時(shí),能對一些輸入輸出參數(shù)進(jìn)行類型轉(zhuǎn)換。 簡單介紹從上面的xml中看到,blog中有author子結(jié)點(diǎn),看起來比較啰嗦,可能我們想把a(bǔ)uthor子節(jié)點(diǎn)作為blog的屬性,如下:blog author=Guilherme Silveira那么就需要用到屬性轉(zhuǎn)換功能。,代碼如下:package 。import 。class AuthorConverter implements SingleValueConverter { public String toString(Object obj) { return ((Author) obj).getName()。 } public Object fromString(String name) { return new Author(name)。 } public boolean canConvert(Class type) { return ()。 }}然后修改主函數(shù),如下:package 。public class Main { public static void main(String[] args) { Blog teamBlog = new Blog(new Author(Guilherme Silveira))。 (new Entry(first, My first blog entry.))。 (new Entry(tutorial, Today we have developed a nice alias tutorial. Tell your friends! NOW!))。 XStream xstream = new XStream()。 (blog, )。 (author, )。 (entry, )。 (author, , writer)。 (, entries)。 (, writer)。 (author, , writer)。 (new AuthorConverter())。 ((teamBlog))。 }}運(yùn)行上面,輸出如下:blog author=Guilherme Silveira entry titlefirst/title descriptionMy first blog entry./description /entry entry titletutorial/title descriptionToday we have developed a nice alias tutorial. Tell your friends! NOW!/description /entry/blog 對象轉(zhuǎn)換器ObjectConverter下面講解ObjectConverter實(shí)例。新建如下兩個(gè)類package 。public class Person { private String name。 public String getName() { return name。 } public void setName(String name) { = name。 }}package 。import 。public class PersonTest { public static void main(String[] args) { Person person = new Person()。 (Guilherme)。 XStream xStream = new XStream()。 (person, )。 ((person))。 }}運(yùn)行以上主函數(shù),輸出結(jié)果如下:person nameGuilherme/name/person現(xiàn)在假如我們需要輸出如下的結(jié)果:person fullnameGuilherme/fullname/person那么最簡單的辦法就是使用aliasFiled方法,如下:package 。import 。public class PersonTest { public static void main(String[] args) { Person person = new Person()。 (Guilherme)。 XStream xStream = new XStream()。 (person, )。 (fullname, , name)。 ((person))。 }}但是,下面我們將通過ObjectConverter實(shí)現(xiàn)上面的功能。新建一個(gè)PersonConverter類,代碼如下:package 。import 。import 。import 。import 。import 。public class PersonConverter implements Converter { public boolean canConvert(Class clazz) { return false。 } public void marshal(Ob