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

假設(shè)輸出如下的XML結(jié)構(gòu),我們應(yīng)該怎么做呢?
<blog author="一個(gè)木瓜"> <entry> <title>第一篇</title> <description>歡迎來到木瓜博客!</description> </entry> <entry> <title>第二篇</title> <description>全國啟動(dòng)防霧霾紅色預(yù)警。</description> </entry> </entries> </blog>
1. 根據(jù)XML結(jié)構(gòu)構(gòu)建JAVA對(duì)象
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.開始代碼測(cè)試
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("一個(gè)木瓜"));
myBlog.add(new Entry("第一篇", "歡迎來到木瓜博客!"));
myBlog.add(new Entry("第二篇", "全國啟動(dòng)防霧霾紅色預(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>一個(gè)木瓜</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>全國啟動(dòng)防霧霾紅色預(yù)警。</description> </com.favccxx.favsoft.pojo.Entry> </entries> </com.favccxx.favsoft.pojo.Blog>
4.使用類別名,將包含路徑的類對(duì)象進(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>一個(gè)木瓜</name> </writer> <entries> <entry> <title>第一篇</title> <description>歡迎來到木瓜博客!</description> </entry> <entry> <title>第二篇</title> <description>全國啟動(dòng)防霧霾紅色預(yù)警。</description> </entry> </entries> </blog>
5. 使用字段別名,將寫手writer改成作者author,發(fā)現(xiàn)輸出結(jié)構(gòu)又近了一層。
xstream.aliasField("author", Blog.class, "writer");<blog> <author> <name>一個(gè)木瓜</name> </author> <entries> <entry> <title>第一篇</title> <description>歡迎來到木瓜博客!</description> </entry> <entry> <title>第二篇</title> <description>全國啟動(dòng)防霧霾紅色預(yù)警。</description> </entry> </entries> </blog>
6. 使用隱式集合,將不需要展示的集合的根節(jié)點(diǎn)進(jìn)行隱藏。需要注意的是數(shù)組和MAP結(jié)合不能聲明成隱式集合。
xstream.addImplicitCollection(Blog.class, "entries");
<blog> <author> <name>一個(gè)木瓜</name> </author> <entry> <title>第一篇</title> <description>歡迎來到木瓜博客!</description> </entry> <entry> <title>第二篇</title> <description>全國啟動(dòng)防霧霾紅色預(yù)警。</description> </entry> </blog>
7. 使用屬性別名,將xml中的成員對(duì)象以屬性標(biāo)簽形式展示。但是屬性標(biāo)簽并不會(huì)直接寫到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("一個(gè)木瓜"));
myBlog.add(new Entry("第一篇", "歡迎來到木瓜博客!"));
myBlog.add(new Entry("第二篇", "全國啟動(dòng)防霧霾紅色預(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="一個(gè)木瓜"> <entry> <title>第一篇</title> <description>歡迎來到木瓜博客!</description> </entry> <entry> <title>第二篇</title> <description>全國啟動(dòng)防霧霾紅色預(yù)警。</description> </entry> </blog>
10.有時(shí)候需要使用包別名,將包名進(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("一個(gè)木瓜"));
myBlog.add(new Entry("第一篇", "歡迎來到木瓜博客!"));
myBlog.add(new Entry("第二篇", "全國啟動(dòng)防霧霾紅色預(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>一個(gè)木瓜</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>全國啟動(dòng)防霧霾紅色預(yù)警。</description> </my.company.favsoft.pojo.Entry> </entries> </my.company.favsoft.pojo.Blog>
小結(jié):
可以使用類別名改變標(biāo)簽的名稱
可以使用字段別名改變標(biāo)簽的名稱
可以使用包別名改變標(biāo)簽的名稱
通過實(shí)現(xiàn)SingleValueConverter接口,可以將成員字段顯示到對(duì)象屬性標(biāo)簽上
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站標(biāo)題:詳解XStream別名-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://www.js-pz168.com/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)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容