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

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

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

假設輸出如下的XML結構,我們應該怎么做呢?

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

1.  根據XML結構構建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("第二篇", "全國啟動防霧霾紅色預警。"));
        XStream xstream = new XStream();
        System.out.println(xstream.toXML(myBlog));
    }
}

3.發(fā)現輸出內容結構并不是想象的那樣,怎么辦?

<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>全國啟動防霧霾紅色預警。</description>
    </com.favccxx.favsoft.pojo.Entry>
  </entries>
</com.favccxx.favsoft.pojo.Blog>

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

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

發(fā)現輸出結構與想要的結構近了一點,但還是不滿足要求。

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

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

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

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

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

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

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

7. 使用屬性別名,將xml中的成員對象以屬性標簽形式展示。但是屬性標簽并不會直接寫到xml標簽上去,需要實現SingleValueConverter轉換器才行。

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("第二篇", "全國啟動防霧霾紅色預警。"));
        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結構

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

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

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("第二篇", "全國啟動防霧霾紅色預警。"));
        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>全國啟動防霧霾紅色預警。</description>
    </my.company.favsoft.pojo.Entry>
  </entries>
</my.company.favsoft.pojo.Blog>

小結:

  • 可以使用類別名改變標簽的名稱

  • 可以使用字段別名改變標簽的名稱

  • 可以使用包別名改變標簽的名稱

  • 通過實現SingleValueConverter接口,可以將成員字段顯示到對象屬性標簽上

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

網站標題:詳解XStream別名-創(chuàng)新互聯(lián)
標題鏈接:http://bm7419.com/article6/hsdog.html

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

廣告

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

成都定制網站建設