详解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,如需转载请自行联系原作者
目录
相关文章
|
负载均衡 监控 算法
实现负载均衡策略:优化系统性能与可用性
实现负载均衡策略:优化系统性能与可用性
|
C# C++ Windows
PDMS call Operating System Command
PDMS call Operating System Command eryar@163.com 1.Introduction AVEVA提供了三种二次开发的方式:DARs, PML和AVEVA .Net(C#)。
2754 0
|
9月前
|
缓存 Prometheus 监控
Python请求淘宝天猫API常见问题解析与解决方案
通过系统化的错误处理机制和性能优化策略,可显著提升淘宝天猫API调用的稳定性和数据获取效率。实际开发中建议结合Prometheus监控调用成功率、错误率等关键指标,构建智能化的API调用管理体系。
|
8月前
|
SQL 人工智能 自然语言处理
业务持续变化时,语义层到底该怎么迭代才不会越改越乱?
业务持续变化时,语义层不能靠“打补丁”硬扛。关键在于构建可分层(对象/关系/属性/规则)、可回溯、可校验的迭代机制,实现映射、口径、对象、知识四类变更分离治理,并配套影响分析与持续校准闭环。
|
缓存 自然语言处理 数据可视化
阿里云百炼产品月刊【2025年7月】
阿里云百炼平台7月推出多项更新与活动,。通义大模型家族迎来五连发升级,推出Qwen-Plus-2025-07-28快照模型,支持1M上下文长度,新增Qwen3-Coder-Plus,具备强大Coding Agent能力。同时,通义万相2.2系列全面升级,文生图、文生视频能力大幅提升。多项模型限时优惠,Qwen3-Coder-Plus最高享5折,Qwen-Plus、Qwen-Turbo价格下调50%,Qwen-MT-Plus降价80%。应用模块新增UI设计器,支持可视化构建Web App。另有多种活动上线,包括Agent创客实训、Qwen3-Coder挑战赛及全栈焕新课程。
2171 2
|
算法 物联网 定位技术
蓝牙室内定位技术解决方案:核心技术架构与优化实践
本文探讨了蓝牙iBeacon与Lora结合的室内定位技术,分析其在复杂室内环境中的优势与挑战。通过三层架构实现高精度定位,并提出硬件、算法与部署优化方向,助力智慧仓储、医疗等场景智能化升级。
768 0
蓝牙室内定位技术解决方案:核心技术架构与优化实践
|
机器学习/深度学习 数据采集 并行计算
面壁小钢炮MiniCPM 4.0开源,端侧推理常规提速5倍!
面壁智能重磅推出MiniCPM 4.0 ——一个极致高效的端侧大模型,通过其 CPM.cu 自研推理框架,可实现220倍极致的速度提升,5 倍常规提速。
1913 14
|
10月前
|
JSON 人工智能 机器人
什么是数据集 —— 大模型微调的 “燃料” 核心解析
本文系统解析大模型微调数据集的核心要点:从定义分类(分类/生成/对话/知识/混合类)、标准格式(JSON/JSONL/CSV),到质量五大要求(准确、相关、多样、无冗余、无偏见),再到构建清洗五步法与电商客服实操案例,助开发者夯实微调基础。(239字)
|
Kubernetes 负载均衡 持续交付
Kubernetes与Docker的关系讲解
Docker像一个“集装箱”,将应用及所需环境打包,确保在任何支持Docker的环境中一致运行。K8s则是“港口管理员”,自动化管理这些“集装箱”的调度、扩展和通信,使开发者能专注于应用开发。两者互补,Docker提供容器化技术,K8s则进行容器编排和管理,共同提升应用的可移植性和稳定性。
847 5