參考資料
1 xStream框架完美實現Java對象和xml文檔JSON、XML相互轉換
http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html
2 xStream完美轉換XML、JSON
http://archive.cnblogs.com/a/2025197/
官網:http://xstream.codehaus.org/download.html
相關jar包可在:
XStream之初識
http://liuzidong.iteye.com/blog/1059453下載
示例代碼
html
Blog teamBlog = new Blog(new Author("Guilherme Silveira")); 函數
teamBlog.add(new Entry("first","My first blog entry.")); ui
eamBlog.add(new Entry("tutorial","Today we have developed a nice alias tutorial. Tell your friends! NOW!")); this
XStream xstream = new XStream(); spa
System.out.println(xstream.toXML(teamBlog)); code
打印結果爲:
xml
<xtream.Blog> 對象
<writer>
<name>Guilherme Silveira</name>
</writer>
<entries>
<xtream.Entry>
<title>first</title>
<description>My first blog entry.</description>
</xtream.Entry>
<xtream.Entry>
<title>tutorial</title>
<description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
</xtream.Entry>
</entries>
</xtream.Blog>
以上節點就包含了包名,如包名很長,就很難看了,怎樣才能從新命名這個節點呀!
如下的1,2設置效果同樣
1 //xstream.aliasPackage("", "xtream");
2 xstream.alias("blog", Blog.class);
3 xstream.alias("entry", Entry.class);
經過這樣設置就完成了節點名稱的設置.以下:
<blog>
<writer>
<name>Guilherme Silveira</name>
</writer>
<entries>
<entry>
<title>first</title>
<description>My first blog entry.</description>
</entry>
<entry>
<title>tutorial</title>
<description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
</entry>
</entries>
</blog>
修改子節點屬性名稱
將writer屬性名稱修改成:autor
xstream.aliasField("author", Blog.class, "writer");
輸出以下:
<blog>
<author>
<name>Guilherme Silveira</name>
</author>
<entries>
<entry>
<title>first</title>
<description>My first blog entry.</description>
</entry>
<entry>
<title>tutorial</title>
<description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
</entry>
</entries>
</blog>
刪除集合節點名稱的設置
xstream.addImplicitCollection(Blog.class, "entries");
輸出以下:
<blog>
<author>
<name>Guilherme Silveira</name>
</author>
<entry>
<title>first</title>
<description>My first blog entry.</description>
</entry>
<entry>
<title>tutorial</title>
<description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
</entry>
</blog>
將Blog類的元素設置爲:它的節點屬性
//使用這個屬性名做爲節點上的元素
xstream.useAttributeFor(Blog.class, "writer");
//從新命名
xstream.aliasField("author", Blog.class, "writer");
//註冊轉換器
xstream.registerConverter(new AuthorConverter());
轉換器:
import com.thoughtworks.xstream.converters.SingleValueConverter;
class AuthorConverter implements SingleValueConverter {
//顯示節點屬性值
public String toString(Object obj) {
return ((Author) obj).getName();
}
public Object fromString(String name) {
return new Author(name);
}
public boolean canConvert(Class type) {
return type.equals(Author.class);
}
}
顯示結果:
<blog author="Guilherme Silveira">
<entry>
<title>first</title>
<description>My first blog entry.</description>
</entry>
<entry>
<title>tutorial</title>
<description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
</entry>
</blog>
註解的使用方式,使用以前必須加上註解類纔有做用:
XStream xstream = new XStream();
xstream.processAnnotations(Blog.class);
xstream.processAnnotations(Entry.class);
1 @XStreamAlias註解可在類與屬性上使用設置名稱,至關於: xstream.alias("blog", Blog.class);
@XStreamAlias("blog")
public class Blog {
@XStreamAlias("author")
private Author writer;
.....
}
固然Entry類也要設置一樣的類屬性:@XStreamAlias("entity")
2 @XStreamImplicit去集合節點名:至關於 xstream.addImplicitCollection(Blog.class, "entries");
3 @XStreamConverter(AuthorConverter.class),參見以上的轉換器類至關於:
xstream.useAttributeFor(Blog.class, "writer");
//從新命名
xstream.aliasField("author", Blog.class, "writer");
//註冊轉換器
xstream.registerConverter(new AuthorConverter());
要使用這個註解AuthorConverter必須符合二個條件
a 必需要有個默認的無參構造函數
public AuthorConverter() {
}
b 類聲明必須爲:public
public class AuthorConverter implements SingleValueConverter {
...
}
不用註解這二點均可以不強制要求!
完整代碼以下:
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
@XStreamAlias("blog")
public class Blog {
@XStreamAsAttribute
@XStreamAlias("author")
@XStreamConverter(AuthorConverter.class)
private Author writer;
@XStreamImplicit
private List entries = new ArrayList();
public Blog(Author writer) {
this.writer = writer;
}
public void add(Entry entry) {
entries.add(entry);
}
public List getContent() {
return entries;
}
public static void main(String[] args) {
Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
teamBlog.add(new Entry("first", "My first blog entry."));
teamBlog
.add(new Entry("tutorial",
"Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
XStream xstream = new XStream();
xstream.processAnnotations(Blog.class);
xstream.processAnnotations(Entry.class);
// 從新命名節點名
// xstream.aliasPackage("", "xtream");
/*
* xstream.alias("blog", Blog.class); xstream.alias("entry",
* Entry.class); //從新命名屬性名 // xstream.aliasField("author", Blog.class,
* "writer"); //去節點 xstream.addImplicitCollection(Blog.class,
* "entries"); // xstream.useAttributeFor(Blog.class, "writer"); //
* xstream.aliasField("author", Blog.class, "writer"); //
* xstream.addImplicitCollection(Blog.class, "entries");
* //使用這個屬性名做爲節點上的元素 xstream.useAttributeFor(Blog.class, "writer");
* //從新命名 xstream.aliasField("author", Blog.class, "writer"); //註冊轉換器
* xstream.registerConverter(new AuthorConverter());
*/
System.out.println(xstream.toXML(teamBlog));
}
}