1. 程式人生 > >使用XStream是實現XML與Java對象的轉換(2)--別名

使用XStream是實現XML與Java對象的轉換(2)--別名

name have ret lis sea sil 數組 ttr auth

五、使用別名(Alias)

首先,有這樣一段Java代碼:

Java代碼
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import com.thoughtworks.xstream.XStream;
  4. public class XStreamTest2 {
  5. public static void main(String[] args) {
  6. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  7. teamBlog.add(new Entry("first","My first blog entry."));
  8. teamBlog.add(new Entry("tutorial",
  9. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  10. XStream xstream = new XStream();
  11. System.out.println(xstream.toXML(teamBlog));
  12. }
  13. }
  14. class Blog {
  15. private Author writer;
  16. private List entries = new ArrayList();
  17. public Blog(Author writer) {
  18. this.writer = writer;
  19. }
  20. public void add(Entry entry) {
  21. entries.add(entry);
  22. }
  23. public List getContent() {
  24. return entries;
  25. }
  26. }
  27. class Author {
  28. private String name;
  29. public Author(String name) {
  30. this.name = name;
  31. }
  32. public String getName() {
  33. return name;
  34. }
  35. }
  36. class Entry {
  37. private String title, description;
  38. public Entry(String title, String description) {
  39. this.title = title;
  40. this.description = description;
  41. }
  42. }

對於上面這段代碼,現在我要將一個Blog對象轉換成為XML字符串,產生的結果是:

Xml代碼
  1. <cn.tjpu.zhw.xml.Blog>
  2. <writer>
  3. <name>Guilherme Silveira</name>
  4. </writer>
  5. <entries>
  6. <cn.tjpu.zhw.xml.Entry>
  7. <title>first</title>
  8. <description>My first blog entry.</description>
  9. </cn.tjpu.zhw.xml.Entry>
  10. <cn.tjpu.zhw.xml.Entry>
  11. <title>tutorial</title>
  12. <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
  13. </cn.tjpu.zhw.xml.Entry>
  14. </entries>
  15. </cn.tjpu.zhw.xml.Blog>

但是,我要將一個Blog對象轉換成為下面的XML字符串的形式:

Xml代碼
  1. <blog author="Guilherme Silveira">
  2. <entry>
  3. <title>first</title>
  4. <description>My first blog entry.</description>
  5. </entry>
  6. <entry>
  7. <title>tutorial</title>
  8. <description>
  9. Today we have developed a nice alias tutorial. Tell your friends! NOW!
  10. </description>
  11. </entry>
  12. </blog>

該怎麽辦呢?

這就需要用到別名轉換方法了!

下面我們就一步步的調整代碼:

1,給類起別名

需求:將節點cn.tjpu.zhw.xml.Blogcn.tjpu.zhw.xml.Entry重命名為blogentry

添加代碼:

Java代碼
  1. xstream.alias("blog", Blog.class);
  2. xstream.alias("entry", Entry.class);

即main方法如下:

Java代碼
  1. public static void main(String[] args) {
  2. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  3. teamBlog.add(new Entry("first","My first blog entry."));
  4. teamBlog.add(new Entry("tutorial",
  5. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  6. XStream xstream = new XStream();
  7. xstream.alias("blog", Blog.class);
  8. xstream.alias("entry", Entry.class);
  9. System.out.println(xstream.toXML(teamBlog));
  10. }

運行結果如下:

Xml代碼
  1. <blog>
  2. <writer>
  3. <name>Guilherme Silveira</name>
  4. </writer>
  5. <entries>
  6. <entry>
  7. <title>first</title>
  8. <description>My first blog entry.</description>
  9. </entry>
  10. <entry>
  11. <title>tutorial</title>
  12. <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
  13. </entry>
  14. </entries>
  15. </blog>

2,給字段其別名

需求:將writer節點重命名為author節點

添加代碼:

Java代碼
  1. xstream.aliasField("author", Blog.class, "writer");

即main方法為:

Java代碼
  1. public static void main(String[] args) {
  2. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  3. teamBlog.add(new Entry("first","My first blog entry."));
  4. teamBlog.add(new Entry("tutorial",
  5. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  6. XStream xstream = new XStream();
  7. xstream.alias("blog", Blog.class);
  8. xstream.alias("entry", Entry.class);
  9. xstream.aliasField("author", Blog.class, "writer");
  10. System.out.println(xstream.toXML(teamBlog));
  11. }

運行結果如下:

Xml代碼
  1. <blog>
  2. <author>
  3. <name>Guilherme Silveira</name>
  4. </author>
  5. <entries>
  6. <entry>
  7. <title>first</title>
  8. <description>My first blog entry.</description>
  9. </entry>
  10. <entry>
  11. <title>tutorial</title>
  12. <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
  13. </entry>
  14. </entries>
  15. </blog>

3,使用隱式集合(Implicit Collection)

隱式集合:當你不想將一個集合的根節點呈現出來的時候,它就是一個隱式集合。

數組、Collection、Map都可以成為隱式集合。

需求:隱藏entries節點

添加代碼:

Java代碼
  1. xstream.addImplicitCollection(Blog.class, "entries");

則main方法如下:

Java代碼
  1. public static void main(String[] args) {
  2. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  3. teamBlog.add(new Entry("first","My first blog entry."));
  4. teamBlog.add(new Entry("tutorial",
  5. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  6. XStream xstream = new XStream();
  7. xstream.alias("blog", Blog.class);
  8. xstream.alias("entry", Entry.class);
  9. xstream.aliasField("author", Blog.class, "writer");
  10. xstream.addImplicitCollection(Blog.class, "entries");
  11. System.out.println(xstream.toXML(teamBlog));
  12. }

運行結果:

Xml代碼
  1. <blog>
  2. <author>
  3. <name>Guilherme Silveira</name>
  4. </author>
  5. <entry>
  6. <title>first</title>
  7. <description>My first blog entry.</description>
  8. </entry>
  9. <entry>
  10. <title>tutorial</title>
  11. <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
  12. </entry>
  13. </blog>

4,給XML屬性起別名

需求:將writer節點,改成blog節點的authot屬性

添加代碼:

Java代碼
  1. xstream.useAttributeFor(Blog.class, "writer");

即main方法如下:

Java代碼
  1. public static void main(String[] args) {
  2. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  3. teamBlog.add(new Entry("first","My first blog entry."));
  4. teamBlog.add(new Entry("tutorial",
  5. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  6. XStream xstream = new XStream();
  7. xstream.alias("blog", Blog.class);
  8. xstream.alias("entry", Entry.class);
  9. xstream.useAttributeFor(Blog.class, "writer");
  10. xstream.aliasField("author", Blog.class, "writer");
  11. xstream.addImplicitCollection(Blog.class, "entries");
  12. System.out.println(xstream.toXML(teamBlog));
  13. }

但是運行的結果卻是不是預想的那樣:

Xml代碼
  1. <blog>
  2. <author>
  3. <name>Guilherme Silveira</name>
  4. </author>
  5. <entry>
  6. <title>first</title>
  7. <description>My first blog entry.</description>
  8. </entry>
  9. <entry>
  10. <title>tutorial</title>
  11. <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
  12. </entry>
  13. </blog>

可以看到,運行的結果根本就沒有變化,為什麽?

因為,還缺少一個轉換器,XML節點的屬性是一個String字符串,但是Author類不是字符串,這就需要一個轉換器將Author對象轉換成一個String對象、同時能夠將String對象反轉換為Author對象:

Java代碼
  1. //定義一個轉換器,如果使用,則需要在使用時註冊到對應的XStream對象中
  2. class AuthorConverter implements SingleValueConverter {
  3. /*該方法用於從Author對象中提取一個String字符串*/
  4. public String toString(Object obj) {
  5. return ((Author) obj).getName();
  6. }
  7. /*該方法用於從一個String字符串生成Author對象*/
  8. public Object fromString(String name) {
  9. return new Author(name);
  10. }
  11. /*該方法告訴XStream對象,該轉換器可以轉換哪種類型的對象*/
  12. public boolean canConvert(Class type) {
  13. return type.equals(Author.class);
  14. }
  15. }

新的main方法如下:

Java代碼
  1. public class XStreamTest2 {
  2. public static void main(String[] args) {
  3. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  4. teamBlog.add(new Entry("first","My first blog entry."));
  5. teamBlog.add(new Entry("tutorial",
  6. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  7. XStream xstream = new XStream();
  8. xstream.alias("blog", Blog.class);
  9. xstream.alias("entry", Entry.class);
  10. //設置XML節點的屬性
  11. xstream.useAttributeFor(Blog.class, "writer");
  12. //註冊轉換器
  13. xstream.registerConverter(new AuthorConverter());
  14. xstream.aliasField("author", Blog.class, "writer");
  15. xstream.addImplicitCollection(Blog.class, "entries");
  16. System.out.println(xstream.toXML(teamBlog));
  17. }
  18. }

到此為止,運行的結果就會真正的向預期的那樣:

Xml代碼
  1. <blog author="Guilherme Silveira">
  2. <entry>
  3. <title>first</title>
  4. <description>My first blog entry.</description>
  5. </entry>
  6. <entry>
  7. <title>tutorial</title>
  8. <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
  9. </entry>
  10. </blog>

5,使用包名別名

需求:有時候需要事項不同包裏面的相同類名的節點之間的相互映射,這時就需要改變一下包名了

添加代碼:

Java代碼
  1. xstream.aliasPackage("org.thoughtworks", "cn.tjpu.zhw.xml");

即更改main方法:

Java代碼
  1. public static void main(String[] args) {
  2. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  3. teamBlog.add(new Entry("first","My first blog entry."));
  4. teamBlog.add(new Entry("tutorial",
  5. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  6. XStream xstream = new XStream();
  7. //包名別名
  8. xstream.aliasPackage("org.thoughtworks", "cn.tjpu.zhw.xml");
  9. System.out.println(xstream.toXML(teamBlog));
  10. }

運行結果如下:

Xml代碼
  1. <org.thoughtworks.Blog>
  2. <writer>
  3. <name>Guilherme Silveira</name>
  4. </writer>
  5. <entries>
  6. <org.thoughtworks.Entry>
  7. <title>first</title>
  8. <description>My first blog entry.</description>
  9. </org.thoughtworks.Entry>
  10. <org.thoughtworks.Entry>
  11. <title>tutorial</title>
  12. <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
  13. </org.thoughtworks.Entry>
  14. </entries>
  15. </org.thoughtworks.Blog>

使用XStream是實現XML與Java對象的轉換(2)--別名