在上一节中,我们已经看到了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,如需转载请自行联系原作者