詳解XStream別名-創(chuàng)新互聯(lián)

在上一節(jié)中,我們已經(jīng)看到了XStream是多么的簡單易用,本文將繼續(xù)以實(shí)例的方式講解XStream別名。畢竟,你的JAVA對象不可能總是與XML結(jié)構(gòu)毫發(fā)無差的,誰也不確定某個開發(fā)者手誤打錯了字母,或者是報文的名稱發(fā)生了變化。

創(chuàng)新互聯(lián)建站于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元建甌做網(wǎng)站,已為上家服務(wù),為建甌各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220

假設(shè)輸出如下的XML結(jié)構(gòu),我們應(yīng)該怎么做呢?

<blog author="一個木瓜">
    <entry>
      <title>第一篇</title>
      <description>歡迎來到木瓜博客!</description>
    </entry>
    <entry>
      <title>第二篇</title>
      <description>全國啟動防霧霾紅色預(yù)警。</description>
    </entry>
  </entries>
</blog>

1.  根據(jù)XML結(jié)構(gòu)構(gòu)建JAVA對象

package com.favccxx.favsoft.pojo;
import java.util.ArrayList;
import java.util.List;
public class Blog {
    private Author writer;
    private List<Entry> entries = new ArrayList<Entry>();
    public Blog(Author writer) {
        this.writer = writer;
    }
    public void add(Entry entry) {
        entries.add(entry);
    }
    public void addEntry(Entry entry){
        entries.add(entry);
    }
    public List<Entry> getContent() {
        return entries;
    }
    public Author getWriter() {
        return writer;
    }
    public void setWriter(Author writer) {
        this.writer = writer;
    }
}

package com.favccxx.favsoft.pojo;
public class Entry {
    private String title;
    private String description;
    public Entry(String title, String description) {
        this.title = title;
        this.description = description;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

2.開始代碼測試

package com.favccxx.favsoft.main;
import com.favccxx.favsoft.pojo.Author;
import com.favccxx.favsoft.pojo.Blog;
import com.favccxx.favsoft.pojo.Entry;
import com.thoughtworks.xstream.XStream;
public class MainBlog {
    public static void main(String args[]) {
        Blog myBlog = new Blog(new Author("一個木瓜"));
        myBlog.add(new Entry("第一篇", "歡迎來到木瓜博客!"));
        myBlog.add(new Entry("第二篇", "全國啟動防霧霾紅色預(yù)警。"));
        XStream xstream = new XStream();
        System.out.println(xstream.toXML(myBlog));
    }
}

3.發(fā)現(xiàn)輸出內(nèi)容結(jié)構(gòu)并不是想象的那樣,怎么辦?

<com.favccxx.favsoft.pojo.Blog>
  <writer>
    <name>一個木瓜</name>
  </writer>
  <entries>
    <com.favccxx.favsoft.pojo.Entry>
      <title>第一篇</title>
      <description>歡迎來到木瓜博客!</description>
    </com.favccxx.favsoft.pojo.Entry>
    <com.favccxx.favsoft.pojo.Entry>
      <title>第二篇</title>
      <description>全國啟動防霧霾紅色預(yù)警。</description>
    </com.favccxx.favsoft.pojo.Entry>
  </entries>
</com.favccxx.favsoft.pojo.Blog>

4.使用類別名,將包含路徑的類對象進(jìn)行替換。

xstream.alias("blog", Blog.class);
xstream.alias("entry", Entry.class);

發(fā)現(xiàn)輸出結(jié)構(gòu)與想要的結(jié)構(gòu)近了一點(diǎn),但還是不滿足要求。

<blog>
  <writer>
    <name>一個木瓜</name>
  </writer>
  <entries>
    <entry>
      <title>第一篇</title>
      <description>歡迎來到木瓜博客!</description>
    </entry>
    <entry>
      <title>第二篇</title>
      <description>全國啟動防霧霾紅色預(yù)警。</description>
    </entry>
  </entries>
</blog>

5. 使用字段別名,將寫手writer改成作者author,發(fā)現(xiàn)輸出結(jié)構(gòu)又近了一層。

xstream.aliasField("author", Blog.class, "writer");

<blog>
  <author>
    <name>一個木瓜</name>
  </author>
  <entries>
    <entry>
      <title>第一篇</title>
      <description>歡迎來到木瓜博客!</description>
    </entry>
    <entry>
      <title>第二篇</title>
      <description>全國啟動防霧霾紅色預(yù)警。</description>
    </entry>
  </entries>
</blog>

6. 使用隱式集合,將不需要展示的集合的根節(jié)點(diǎn)進(jìn)行隱藏。需要注意的是數(shù)組和MAP結(jié)合不能聲明成隱式集合。

xstream.addImplicitCollection(Blog.class, "entries");

<blog>
  <author>
    <name>一個木瓜</name>
  </author>
  <entry>
    <title>第一篇</title>
    <description>歡迎來到木瓜博客!</description>
  </entry>
  <entry>
    <title>第二篇</title>
    <description>全國啟動防霧霾紅色預(yù)警。</description>
  </entry>
</blog>

7. 使用屬性別名,將xml中的成員對象以屬性標(biāo)簽形式展示。但是屬性標(biāo)簽并不會直接寫到xml標(biāo)簽上去,需要實(shí)現(xiàn)SingleValueConverter轉(zhuǎn)換器才行。

xstream.useAttributeFor(Blog.class, "writer");
xstream.aliasField("author", Blog.class, "writer");

package com.favccxx.favsoft.util;
import com.favccxx.favsoft.pojo.Author;
import com.thoughtworks.xstream.converters.SingleValueConverter;
public class AuthorConverter implements SingleValueConverter {
    @Override
    public boolean canConvert(Class type) {
        return type.equals(Author.class);
    }
    @Override
    public String toString(Object obj) {
        return ((Author) obj).getName();
    }
    @Override
    public Object fromString(String str) {
        return new Author(str);
    }
}

8.終于改完了,最終的完整代碼是這樣的

package com.favccxx.favsoft.main;
import com.favccxx.favsoft.pojo.Author;
import com.favccxx.favsoft.pojo.Blog;
import com.favccxx.favsoft.pojo.Entry;
import com.favccxx.favsoft.util.AuthorConverter;
import com.thoughtworks.xstream.XStream;
public class MainBlog {
    public static void main(String args[]) {
        Blog myBlog = new Blog(new Author("一個木瓜"));
        myBlog.add(new Entry("第一篇", "歡迎來到木瓜博客!"));
        myBlog.add(new Entry("第二篇", "全國啟動防霧霾紅色預(yù)警。"));
        XStream xstream = new XStream();
        xstream.alias("blog", Blog.class);
        xstream.alias("entry", Entry.class);
        xstream.useAttributeFor(Blog.class, "writer");
        xstream.aliasField("author", Blog.class, "writer");
        xstream.registerConverter(new AuthorConverter());
        xstream.addImplicitCollection(Blog.class, "entries");
        System.out.println(xstream.toXML(myBlog));
    }
}

9.輸出完美的XML結(jié)構(gòu)

<blog author="一個木瓜">
  <entry>
    <title>第一篇</title>
    <description>歡迎來到木瓜博客!</description>
  </entry>
  <entry>
    <title>第二篇</title>
    <description>全國啟動防霧霾紅色預(yù)警。</description>
  </entry>
</blog>

10.有時候需要使用包別名,將包名進(jìn)行替換。需要替換的包名必須從首字母開始替換,不能從中間開始查找替換。

package com.favccxx.favsoft.main;
import com.favccxx.favsoft.pojo.Author;
import com.favccxx.favsoft.pojo.Blog;
import com.favccxx.favsoft.pojo.Entry;
import com.thoughtworks.xstream.XStream;
public class MainBlog {
    public static void main(String args[]) {
        Blog myBlog = new Blog(new Author("一個木瓜"));
        myBlog.add(new Entry("第一篇", "歡迎來到木瓜博客!"));
        myBlog.add(new Entry("第二篇", "全國啟動防霧霾紅色預(yù)警。"));
        XStream xstream = new XStream();
        xstream.aliasPackage("my.company", "com.favccxx");
        System.out.println(xstream.toXML(myBlog));
    }
}

11.輸出格式如下

<my.company.favsoft.pojo.Blog>
  <writer>
    <name>一個木瓜</name>
  </writer>
  <entries>
    <my.company.favsoft.pojo.Entry>
      <title>第一篇</title>
      <description>歡迎來到木瓜博客!</description>
    </my.company.favsoft.pojo.Entry>
    <my.company.favsoft.pojo.Entry>
      <title>第二篇</title>
      <description>全國啟動防霧霾紅色預(yù)警。</description>
    </my.company.favsoft.pojo.Entry>
  </entries>
</my.company.favsoft.pojo.Blog>

小結(jié):

  • 可以使用類別名改變標(biāo)簽的名稱

  • 可以使用字段別名改變標(biāo)簽的名稱

  • 可以使用包別名改變標(biāo)簽的名稱

  • 通過實(shí)現(xiàn)SingleValueConverter接口,可以將成員字段顯示到對象屬性標(biāo)簽上

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站標(biāo)題:詳解XStream別名-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://muchs.cn/article6/hsdog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、搜索引擎優(yōu)化、服務(wù)器托管、企業(yè)建站用戶體驗(yàn)、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)