XStream--對(duì)象與XML的互轉(zhuǎn).doc_第1頁(yè)
XStream--對(duì)象與XML的互轉(zhuǎn).doc_第2頁(yè)
XStream--對(duì)象與XML的互轉(zhuǎn).doc_第3頁(yè)
XStream--對(duì)象與XML的互轉(zhuǎn).doc_第4頁(yè)
XStream--對(duì)象與XML的互轉(zhuǎn).doc_第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

XStream使用方法總結(jié)附實(shí)例代碼XStream中的核心類(lèi)就是XStream類(lèi),一般來(lái)說(shuō),熟悉這個(gè)類(lèi)基本就夠用了,如果你用的更多,估計(jì)是你設(shè)計(jì)有問(wèn)題,否則不需要。 XStream對(duì)象相當(dāng)Java對(duì)象和XML之間的轉(zhuǎn)換器,轉(zhuǎn)換過(guò)程是雙向的。創(chuàng)建XSteam對(duì)象的方式很簡(jiǎn)單,只需要new XStream()即可。 Java到xml,用toXML()方法。 Xml到Java,用fromXML()方法。 在沒(méi)有任何設(shè)置默認(rèn)情況下,java到xml的映射,是java成員名對(duì)應(yīng)xml的元素名,java類(lèi)的全名對(duì)應(yīng)xml根元素的名字。而實(shí)際中,往往是xml和java類(lèi)都有了,要完成相互轉(zhuǎn)換,必須進(jìn)行別名映射。 別名配置包含三種情況: 1、類(lèi)別名,用alias(String name, Class type)。 2、類(lèi)成員別名,用aliasField(String alias, Class definedIn, String fieldName) 3、 類(lèi)成員作為屬性別名,用 aliasAttribute(Class definedIn, String attributeName, String alias),單獨(dú)命名沒(méi)有意義,還要通過(guò)useAttributeFor(Class definedIn, String fieldName) 應(yīng)用到某個(gè)類(lèi)上。 別名的配置是非常重要的,但是其中有些細(xì)節(jié)問(wèn)題很重要,在例子中會(huì)專門(mén)做詳細(xì)說(shuō)明。 另外還有不太常用的方法: addImplicitCollection(Class ownerType, String fieldName),去掉集合類(lèi)型生成xml的父節(jié)點(diǎn)。 registerConverter(Converter converter) ,注冊(cè)一個(gè)轉(zhuǎn)換器。 如果你的xml很大,或者為了安全性,以流的方式傳輸,那么XStream也提供豐富的API, 使用起來(lái)也非常簡(jiǎn)便。目前還用不到,暫不考慮。 如 果這些基本的操作還不能滿足你應(yīng)用的需求,XStream提供豐富的擴(kuò)展點(diǎn)。你可以實(shí)現(xiàn)自己的轉(zhuǎn)換器。還可以利用XStream完成更負(fù)責(zé)的功能,比如輸 出其他非xml格式的數(shù)據(jù),還可以輸出html,還支持XML Dom類(lèi)型數(shù)據(jù),這些應(yīng)用起來(lái)稍微復(fù)雜些。當(dāng)然這些不是XStream應(yīng)用的重點(diǎn),也不用理會(huì),真正需要的時(shí)候再查看API和源碼研究研究。 XStream的優(yōu)點(diǎn)很多,但是也有一些小bug,比如在定義別名中的下劃線“_”轉(zhuǎn)換為xml后會(huì)變成“_”這個(gè)符號(hào),很變態(tài)。因此,盡量避免在別名中實(shí)用任何符號(hào),卻是需要下劃線的時(shí)候,可以考慮實(shí)用連接符“-”,這個(gè)沒(méi)有問(wèn)題。 另外,我們的Java Bean中,常常有一些常量,在轉(zhuǎn)換過(guò)程,XStream也會(huì)將這些常量轉(zhuǎn)換過(guò)去,形成常量的xml節(jié)點(diǎn),這顯然不是想要的結(jié)果,對(duì)于常量字段,就不做轉(zhuǎn)換了。 下面給出一個(gè)非常典型的而且實(shí)用的例子,作為對(duì)總結(jié)的補(bǔ)充: package test; import java.util.List; /* * Created by IntelliJ IDEA. * User: leizhimin * Date: 2008-5-22 21:10:13 * Note: Please add comment here! */ public class Person private String name; private String age; private Profile profile; private List addlist; public Person(String name, String age, Profile profile, List addlist) = name; this.age = age; file = profile; this.addlist = addlist; public String toString() return Person + name= + name + + , age= + age + + , profile= + profile + , addlist= + addlist + ; package test; import java.sql.Date; /* * Created by IntelliJ IDEA. * User: leizhimin * Date: 2008-5-22 21:10:32 * Note: Please add comment here! */ public class Profile private String job; private String tel; private String remark; public Profile(String job, String tel, String remark) this.job = job; this.tel = tel; this.remark = remark; public String toString() return Profile + job= + job + + , tel= + tel + + , remark= + remark + + ; package test; /* * Created by IntelliJ IDEA. * User: leizhimin * Date: 2008-5-22 21:10:22 * Note: Please add comment here! */ public class Address private String add; private String zipcode; public Address(String add, String zipcode) this.add = add; this.zipcode = zipcode; public String toString() return Address + add= + add + + , zipcode= + zipcode + + ; package test; import com.thoughtworks.xstream.XStream; import java.util.List; import java.util.ArrayList; /* * Created by IntelliJ IDEA. * User: leizhimin * Date: 2008-5-22 21:10:47 * Note: XStream學(xué)習(xí) */ public class TestXStream public static void main(String args) test(); public static void test() System.out.println(-XStream學(xué)習(xí):-); /目標(biāo)對(duì)象 Address address1 = new Address(鄭州市經(jīng)三路, 450001); Address address2 = new Address(西安市雁塔路, 710002); List addList = new ArrayList(); addList.add(address1); addList.add(address2); Profile profile = new Profile(軟件工程師, 備注說(shuō)明); Person person = new Person(熔巖, 27, profile, addList); /轉(zhuǎn)換裝配 XStream xStream = new XStream(); /* 設(shè)置類(lèi)別名 */ xStream.alias(PERSON, test.Person.class); xStream.alias(PROFILE, test.Profile.class); xStream.alias(ADDRESS, test.Address.class); output(1, xStream, person); /* 設(shè)置類(lèi)成員的別名 */ /設(shè)置Person類(lèi)的name成員別名Name xStream.aliasField(Name, Person.class, name); /*注意 設(shè)置Person類(lèi)的profile成員別名PROFILE,這個(gè)別名和Profile類(lèi)的別名一致, * 這樣可以保持XStream對(duì)象可以從profile成員生成的xml片段直接轉(zhuǎn)換為Profile成員, * 如果成員profile的別名和Profile的別名不一致,則profile成員生成的xml片段不可 * 直接轉(zhuǎn)換為Profile對(duì)象,需要重新創(chuàng)建XStream對(duì)象,這豈不給自己找麻煩? */ xStream.aliasField(PROFILE, test.Person.class, profile); xStream.aliasField(ADDLIST, test.Person.class, addlist); xStream.aliasField(Add, test.Address.class, add); xStream.aliasField(Job, test.Profile.class, job); output(2, xStream, person); /* 設(shè)置類(lèi)成員為xml一個(gè)元素上的屬性 */ xStream.useAttributeFor(Address.class, zipcode); /* 設(shè)置屬性的別名 */ xStream.aliasAttribute(test.Address.class, zipcode, Zipcode); output(3, xStream, person); /* 將xml轉(zhuǎn)為java對(duì)象 */ String person_xml = n + 熔巖n + 27n + n + 軟件工程師n + + 備注說(shuō)明n + n + n + n + 鄭州市經(jīng)三路n + n + n + 西安市雁塔路n + n + n + ; String profile_xml = n + 軟件工程師n + + 備注說(shuō)明n + ; String address_xml = n + 西安市雁塔路n + ; /同樣實(shí)用上面的XStream對(duì)象xStream System.out.println(xStream.fromXML(person_xml).toString(); System.out.println(xStream.fromXML(profile_xml).toString(); System.out.println(xStream.fromXML(address_xml).toString(); public static void output(int i, XStream xStream, Object obj) String xml = xStream.toXML(obj); System.out.println(第 + i + 次輸出n); System.out.println(xml + n); -XStream學(xué)習(xí):- 第 1次輸出 熔巖 27 軟件工程師備注說(shuō)明 鄭州市經(jīng)三路 450001 西安市雁塔路 710002 第 2次輸出 熔巖 27 軟件工程師備注說(shuō)明 鄭州市經(jīng)三路 450001 西安市雁塔路 710002 第 3次輸出 熔巖 27 軟件工程師備注說(shuō)明 鄭州市經(jīng)三路 西安市雁塔路 Personname= 熔巖, age=27, profile=Profilejob=軟件工程師, tel remark=備注說(shuō)明, addlist=Addressadd=鄭州市經(jīng)三路, zipcode=450001, Addressadd=西安市雁塔路, zipcode=710002 Profilejob=軟件工程師, tel remark=備注說(shuō)明 Addressadd=西安市雁塔路, zipcode=710002 Process finished with exit code 0 在 實(shí)際中,類(lèi)的屬性很多,嵌套層次也很復(fù)雜,如果僅僅使用XStream原生API來(lái)硬編碼設(shè)置別名等屬性,顯得太生硬也難以維護(hù)。完全可以考慮通過(guò)一個(gè) xml配置文件來(lái)定義所有用到的類(lèi)的別名定義(包括其成員),然后,通過(guò)讀取配置構(gòu)建一個(gè)XStream的工廠,在用到時(shí)候直接去取,而不是讓實(shí)用者組 裝。我目前的一個(gè)項(xiàng)目中,就是這么實(shí)現(xiàn)的,效果非常的好。 下面我給出針對(duì)上面提出的問(wèn)題一個(gè)解決方案: 思想:考慮做一個(gè)過(guò)濾器,在xml轉(zhuǎn)java之前,在Java轉(zhuǎn)xml之后,應(yīng)用這個(gè)過(guò)濾器。這個(gè)過(guò)濾器提供將xml中的“_”替換為“-”,并且將xml中的不需要的節(jié)點(diǎn)剔除。 在過(guò)濾之前,我實(shí)現(xiàn)了個(gè)轉(zhuǎn)換器裝配,這一步通過(guò)xml來(lái)配置,并在java中獲取。 代碼就省略了,這一步很靈活,關(guān)鍵看你的應(yīng)用了。 為了能過(guò)濾xml,我們需要用Dom4j遞歸遍歷xml文檔。下面一些算法代碼: /遞歸算法:遍歷配置文件,找出所有有效的xpath private static void recursiveElement(Element element) List elements = element.elements(); validXPathList.add(element.getPath(); if (elements.size() = 0) /沒(méi)有子元素 else /有子元素 for (Iterator it = elements.iterator(); it.hasNext();) /遞歸遍歷 recursiveElement(it.next(); /遞歸算法:遍歷xml,標(biāo)識(shí)無(wú)效的元素節(jié)點(diǎn) private static void recursiveFixElement(Element element) List elements = element.elements(); if (!validXPathList.contains(element.getPath() element.addAttribute(delete, true); if (elements.size() = 0) /沒(méi)有子元素 els

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論