XStream xml 與 實體類 的相互轉(zhuǎn)換

XStream 簡介

XStream是一款能夠非常簡單實現(xiàn) XML與 實體類 之間轉(zhuǎn)換的類庫若厚,下文就簡單地記錄下其主要使用方法痴昧。
直接上代碼

一 實體轉(zhuǎn)XML

1.1 簡單使用

public class Student {
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
Student student=new Student("matt",18);
XStream xStream=new XStream();
String xmlStudent = xStream.toXML(student);

以下為輸入結果:

<com.laxton.rms.upload.bean.Student>
  <age>18</age>
  <name>matt</name>
</com.laxton.rms.upload.bean.Student>

1.2 alias

有沒有覺得com.laxton.rms.upload.bean.Student很礙眼煎楣,我們可以通過關鍵字alias更改給它換一個別名。

Student student=new Student("matt",18);
XStream xStream=new XStream();
xStream.alias("Student",Student.class);//為類名節(jié)點重命名
String xmlStudent = xStream.toXML(student);

以下為輸入結果:

<Student>
  <age>18</age>
  <name>matt</name>
</Student>

這樣就舒服多了署恍。

1.3 useAttributeFor

<Student atrr="Attribute">
  <age>18</age>
 <name>matt</name>
</Student>

這個XML的格式跟上一個是有區(qū)別的隔显,在Student這個節(jié)點里多了一個atrr="Attribute"却妨,那么我們要怎么添加這樣的格式呢?
首先括眠,在Student實體類里添加一個字段:atrr

public class Student {
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }
    public Student(String name,int age,String atrr){
        this.name=name;
        this.age=age;
        this.atrr=atrr;
    }
    private String name;
    private int age;
    private String atrr;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getAtrr() {
        return atrr;
    }
    public void setAtrr(String atrr) {
        this.atrr = atrr;
    }

然后彪标,使用useAttributeFor關鍵字

Student student=new Student("matt",18,"Attribute");
XStream xStream=new XStream();
xStream.alias("Student",Student.class);//為類名節(jié)點重命名
xStream.useAttributeFor(Student.class,"atrr");
String xmlStudent = xStream.toXML(student);

輸出結果如下:

<Student atrr="Attribute">
  <age>18</age>
 <name>matt</name>
</Student>

1.4 問題:當實體類的某個值為null時,轉(zhuǎn)換的xml會收起這個節(jié)點不顯示

Student student=new Student(null,18);
XStream xStream=new XStream();
xStream.alias("Student",Student.class);//為類名節(jié)點重命名
String xmlStudent = xStream.toXML(student);

結果輸出:

<Student>
       <age>18</age>
</Student>

在這個例子中掷豺,當我們給name賦予null值時捞烟,輸出的xml文本沒有 <name> 這個節(jié)點。網(wǎng)絡上有相關文章描述了如何通過Converter將賦為null值的字段依然顯示節(jié)點当船。
但本人覺得完全沒有必要這么做题画。因為當你將沒有 <name>節(jié)點的xml文本轉(zhuǎn)換為實體時,name字段會被賦予null值德频,這種相互之間的轉(zhuǎn)換是完全匹配的苍息。如果你將<name>節(jié)點強制顯示出來,當它轉(zhuǎn)換成實體類時壹置,name字段會被賦予空值竞思,反而與實際情況不符。

二 XML轉(zhuǎn)實體類

2.1 簡單使用

當我企圖用以下的xml文本轉(zhuǎn)換成實體類Student時報錯钞护。

<Student>
  <age>18</age>
 <name>matt</name>
</Student>
  String xmlStr = "<Student>\n" +
                "  <age>18</age>\n" +
                "  <name>matt</name>\n" +
                "</Student>";

        XStream xStream=new XStream();
        Student student = (Student)xStream.fromXML(xmlStr);
        Log.i("vlog","name:"+student.getName());
        Log.i("vlog","age:"+student.getAge());

恍然大悟后改成:

  String xmlStr = "<com.laxton.rms.upload.beanStudent>\n" +
                "  <age>18</age>\n" +
                "  <name>matt</name>\n" +
                "</com.laxton.rms.upload.beanStudent>";
   ....

就能成功地轉(zhuǎn)換盖喷,可現(xiàn)實中的xml文件是不會帶類名的。

2.2 alias

所以跟實體類轉(zhuǎn)XML一個道理 患亿,我們需要用到alias關鍵字传蹈。

  String xmlStr = "<Student>\n" +
                "  <age>18</age>\n" +
                "  <name>matt</name>\n" +
                "</Student>";

        XStream xStream=new XStream();
       xStream.alias("Student",Student.class);
        Student student = (Student)xStream.fromXML(xmlStr);
        Log.i("vlog","name:"+student.getName());
        Log.i("vlog","age:"+student.getAge());

完美解決問題。

2.3 useAttributeFor

如果遇到如下xml文本步藕,應當怎樣將它轉(zhuǎn)換為實體類呢惦界?

      String xmlStr = "<Student atrr=\"Attribute\">\n" +
                "  <age>18</age>\n" +
                "  <name>matt</name>\n" +
                "</Student>";

跟上面的xml文本相比,這一個多了一個Student節(jié)點下的屬性:<Student atrr="Attribute">咙冗。
使用useAttributeFor關鍵字沾歪,完美解決問題,直接上代碼 :

  String xmlStr = "<Student atrr=\"Attribute\">\n" +
                "  <age>18</age>\n" +
                "  <name>matt</name>\n" +
                "</Student>";

        XStream xStream=new XStream();
        xStream.alias("Student",Student.class);
        xStream.useAttributeFor(Student.class,"atrr");
        Student student = (Student)xStream.fromXML(xmlStr);
        Log.i("vlog","name:"+student.getName());
        Log.i("vlog","age:"+student.getAge());
        Log.i("vlog","atrr:"+student.getAtrr());

2.4 問題:xStream.fromXML(File)與xStream.fromXML(String)

問題描述:由.net服務器端生成的一份xml文件雾消,我將它拷貝放到安卓設備的SD卡灾搏,再將它的xml文本讀出來。嘗試用xStream.fromXML(String)的方法將其轉(zhuǎn)成對象立润,但報錯了狂窑,具體錯誤如下:

 Caused by: org.xmlpull.v1.XmlPullParserException: Unexpected token (position:TEXT @1:2 in java.io.StringReader@888018) 
          at org.kxml2.io.KXmlParser.next(KXmlParser.java:432)
          at org.kxml2.io.KXmlParser.next(KXmlParser.java:313)

找不著具體原因,也找不到解決辦法桑腮。
突然看到還有xStream.fromXML(File)這個接口泉哈,于是直接用xml文件的存放路徑new一個File再調(diào)用該接口,能夠順利轉(zhuǎn)換不會報錯破讨。
問題得以解決但并不清楚其它內(nèi)在原因丛晦。
嘗試過手動將這個文件的文本拷貝出來,新建另一個文件提陶,將文本粘貼進去烫沙。然后使用xStream.fromXML(String)進行轉(zhuǎn)換,也能順利轉(zhuǎn)換隙笆。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末锌蓄,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子仲器,更是在濱河造成了極大的恐慌煤率,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件乏冀,死亡現(xiàn)場離奇詭異蝶糯,居然都是意外死亡,警方通過查閱死者的電腦和手機辆沦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門昼捍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人肢扯,你說我怎么就攤上這事妒茬。” “怎么了蔚晨?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵乍钻,是天一觀的道長肛循。 經(jīng)常有香客問我,道長银择,這世上最難降的妖魔是什么多糠? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮浩考,結果婚禮上夹孔,老公的妹妹穿的比我還像新娘。我一直安慰自己析孽,他們只是感情好搭伤,可當我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著袜瞬,像睡著了一般怜俐。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上邓尤,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天佑菩,我揣著相機與錄音,去河邊找鬼裁赠。 笑死殿漠,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的佩捞。 我是一名探鬼主播绞幌,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼一忱!你這毒婦竟也來了莲蜘?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤帘营,失蹤者是張志新(化名)和其女友劉穎票渠,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體芬迄,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡问顷,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了禀梳。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片杜窄。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖算途,靈堂內(nèi)的尸體忽然破棺而出塞耕,到底是詐尸還是另有隱情,我是刑警寧澤嘴瓤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布扫外,位于F島的核電站莉钙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏筛谚。R本人自食惡果不足惜胆胰,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望刻获。 院中可真熱鬧,春花似錦瞎嬉、人聲如沸蝎毡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽沐兵。三九已至,卻和暖如春便监,著一層夾襖步出監(jiān)牢的瞬間扎谎,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工烧董, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留十籍,地道東北人肋演。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親簿寂。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理颗胡,服務發(fā)現(xiàn)们何,斷路器,智...
    卡卡羅2017閱讀 134,701評論 18 139
  • 1. Java基礎部分 基礎部分的順序:基本語法扇商,類相關的語法凤瘦,內(nèi)部類的語法,繼承相關的語法案铺,異常的語法蔬芥,線程的語...
    子非魚_t_閱讀 31,663評論 18 399
  • 新媒體時代爆文的核心要素: 1、必須在思維層面吃透系列技巧控汉,融會貫通坝茎,做到本能的應用技巧; 2暇番、高效率嗤放,快速度的為...
    狐貍也糊涂plus閱讀 445評論 0 0
  • 時間是十月底至十一月初,今天我剛從美不勝收的敦煌回到黃河穿城而過的城市—蘭州壁酬。敦煌于我的總體印象就是風景美次酌,人心美...
    ExtendNo1閱讀 299評論 0 2