詳解XStream別名

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


假設輸出如下的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.發現輸出內容結構並不是想象的那樣,怎麼辦?

<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);


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

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


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

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. 使用隱式集合,將不需要展示的集合的根節點進行隱藏。需要注意的是數組和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接口,可以將成員字段顯示到對象屬性標籤上


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章