详解XStream别名

简介:

在上一节中,我们已经看到了XStream是多么的简单易用,本文将继续以实例的方式讲解XStream别名。毕竟,你的JAVA对象不可能总是与XML结构毫发无差的,谁也不确定某个开发者手误打错了字母,或者是报文的名称发生了变化。


假设输出如下的XML结构,我们应该怎么做呢?

1
2
3
4
5
6
7
8
9
10
11
< blog  author = "一个木瓜" >
     < entry >
       < title >第一篇</ title >
       < description >欢迎来到木瓜博客!</ description >
     </ entry >
     < entry >
       < title >第二篇</ title >
       < description >全国启动防雾霾红色预警。</ description >
     </ entry >
   </ entries >
</ blog >


1.  根据XML结构构建JAVA对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
     }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.开始代码测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.发现输出内容结构并不是想象的那样,怎么办?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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.使用类别名,将包含路径的类对象进行替换。

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


发现输出结构与想要的结构近了一点,但还是不满足要求。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< blog >
   < writer >
     < name >一个木瓜</ name >
   </ writer >
   < entries >
     < entry >
       < title >第一篇</ title >
       < description >欢迎来到木瓜博客!</ description >
     </ entry >
     < entry >
       < title >第二篇</ title >
       < description >全国启动防雾霾红色预警。</ description >
     </ entry >
   </ entries >
</ blog >


5. 使用字段别名,将写手writer改成作者author,发现输出结构又近了一层。

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< blog >
   < author >
     < name >一个木瓜</ name >
   </ author >
   < entries >
     < entry >
       < title >第一篇</ title >
       < description >欢迎来到木瓜博客!</ description >
     </ entry >
     < entry >
       < title >第二篇</ title >
       < description >全国启动防雾霾红色预警。</ description >
     </ entry >
   </ entries >
</ blog >


6. 使用隐式集合,将不需要展示的集合的根节点进行隐藏。需要注意的是数组和MAP结合不能声明成隐式集合。

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


1
2
3
4
5
6
7
8
9
10
11
12
13
< blog >
   < author >
     < name >一个木瓜</ name >
   </ author >
   < entry >
     < title >第一篇</ title >
     < description >欢迎来到木瓜博客!</ description >
   </ entry >
   < entry >
     < title >第二篇</ title >
     < description >全国启动防雾霾红色预警。</ description >
   </ entry >
</ blog >


7. 使用属性别名,将xml中的成员对象以属性标签形式展示。但是属性标签并不会直接写到xml标签上去,需要实现SingleValueConverter转换器才行。

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.终于改完了,最终的完整代码是这样的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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结构

1
2
3
4
5
6
7
8
9
10
< blog  author = "一个木瓜" >
   < entry >
     < title >第一篇</ title >
     < description >欢迎来到木瓜博客!</ description >
   </ entry >
   < entry >
     < title >第二篇</ title >
     < description >全国启动防雾霾红色预警。</ description >
   </ entry >
</ blog >


10.有时候需要使用包别名,将包名进行替换。需要替换的包名必须从首字母开始替换,不能从中间开始查找替换。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.输出格式如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< 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接口,可以将成员字段显示到对象属性标签上





本文转自 genuinecx 51CTO博客,原文链接:http://blog.51cto.com/favccxx/1884457,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
Java 数据库连接
hibernate注解实体类(Dept.java)
hibernate注解实体类(Dept.java)
13 1
|
6月前
|
JSON Java 数据格式
【Java反序列化】@JsonAlias字段别名
 @JsonAlias 是 Jackson 库提供的一个注解,用于在反序列化 JSON 数据时,为字段或方法指定多个可接受的名称或别名。
138 0
|
4月前
|
存储 Java 关系型数据库
MyBatisPlus中的TypeHandler | 自定义字段类型转换Handler
MyBatisPlus中的TypeHandler | 自定义字段类型转换Handler
152 0
|
1月前
|
Java 数据库连接
hibernate注解实体类(Emp.java)
hibernate注解实体类(Emp.java)
14 1
|
18天前
|
Java 数据库连接 mybatis
mybatis 查询结果返回 list<pojo> 和 一个 pojo 类 的一些坑
mybatis 查询结果返回 list<pojo> 和 一个 pojo 类 的一些坑
52 0
|
5月前
|
Java
springboot中VO属性别名
springboot中VO属性别名
48 0
|
10月前
|
开发框架 安全 Java
|
11月前
|
XML 存储 Java
Spring OXM-XStream使用别名
Spring OXM-XStream使用别名
67 0
|
Java Spring
Spring配置详解,别名和导入
1.别名 例如:给hello取一个别名叫hey:
170 1
|
Java 数据库连接 mybatis
mybatis学习(17):列名与属性名不一致的情况(使用别名)
mybatis学习(17):列名与属性名不一致的情况(使用别名)
70 0
mybatis学习(17):列名与属性名不一致的情况(使用别名)