XStream
1.引入需要的jar包,在pom.xml中配置依赖
- <dependency>
- <groupId>com.thoughtworks.xstream</groupId>
- <artifactId>xstream</artifactId>
- <version></version>
- </dependency>
<dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.2</version> </dependency>
2.创建要转换的实例类,本文以简单的硬件信息为例
- package com.ajita.xstream;
- import com.thoughtworks.xstream.annotations.XStreamAlias;
- import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
- @XStreamAlias("DiskInfo")
- public class DiskInfo {
- @XStreamAsAttribute
- @XStreamAlias("diskName")
- private String name;
- private int totalSize;
- private int avilableSize;
- private int usedSize;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getTotleSize() {
- return totalSize;
- }
- public void setTotleSize(int totleSize) {
- this.totalSize = totleSize;
- }
- public int getAvilableSize() {
- return avilableSize;
- }
- public void setAvilableSize(int avilableSize) {
- this.avilableSize = avilableSize;
- }
- public int getUserSize() {
- return usedSize;
- }
- public void setUserSize(int userSize) {
- this.usedSize = userSize;
- }
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(name + "\r\n");
- sb.append("TotalSize:" + totalSize + "|Used Size:" + usedSize
- + "|Available Size:" + avilableSize);
- return sb.toString();
- }
- }
- package com.ajita.xstream;
- import java.util.List;
- import com.thoughtworks.xstream.annotations.XStreamAlias;
- @XStreamAlias("HardwareInfo")
- public class HardwareInfo {
- private String cpuInfo;
- private String networkInfo;
- private List<DiskInfo> diskInfos;
- public String getCpuInfo() {
- return cpuInfo;
- }
- public void setCpuInfo(String cpuInfo) {
- this.cpuInfo = cpuInfo;
- }
- public String getNetworkInfo() {
- return networkInfo;
- }
- public void setNetworkInfo(String networkInfo) {
- this.networkInfo = networkInfo;
- }
- public List<DiskInfo> getDiskInfos() {
- return diskInfos;
- }
- public void setDiskInfos(List<DiskInfo> diskInfos) {
- this.diskInfos = diskInfos;
- }
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(cpuInfo);
- sb.append("\r\n");
- sb.append(networkInfo);
- sb.append("\r\n");
- for (DiskInfo disk : diskInfos) {
- sb.append(disk.toString());
- sb.append("\r\n");
- }
- return sb.toString();
- }
- }
package com.ajita.xstream; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAsAttribute; @XStreamAlias("DiskInfo") public class DiskInfo { @XStreamAsAttribute @XStreamAlias("diskName") private String name; private int totalSize; private int avilableSize; private int usedSize; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTotleSize() { return totalSize; } public void setTotleSize(int totleSize) { this.totalSize = totleSize; } public int getAvilableSize() { return avilableSize; } public void setAvilableSize(int avilableSize) { this.avilableSize = avilableSize; } public int getUserSize() { return usedSize; } public void setUserSize(int userSize) { this.usedSize = userSize; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append(name + "\r\n"); sb.append("TotalSize:" + totalSize + "|Used Size:" + usedSize + "|Available Size:" + avilableSize); return sb.toString(); } } package com.ajita.xstream; import java.util.List; import com.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("HardwareInfo") public class HardwareInfo { private String cpuInfo; private String networkInfo; private List<DiskInfo> diskInfos; public String getCpuInfo() { return cpuInfo; } public void setCpuInfo(String cpuInfo) { this.cpuInfo = cpuInfo; } public String getNetworkInfo() { return networkInfo; } public void setNetworkInfo(String networkInfo) { this.networkInfo = networkInfo; } public List<DiskInfo> getDiskInfos() { return diskInfos; } public void setDiskInfos(List<DiskInfo> diskInfos) { this.diskInfos = diskInfos; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append(cpuInfo); sb.append("\r\n"); sb.append(networkInfo); sb.append("\r\n"); for (DiskInfo disk : diskInfos) { sb.append(disk.toString()); sb.append("\r\n"); } return sb.toString(); } }
3.新建测试代码
- package com.ajita.xstream;
- import java.util.ArrayList;
- import com.thoughtworks.xstream.XStream;
- public class TestXStream {
- /**
- * @param args
- */
- public static void main(String[] args) {
- DiskInfo disk1 = new DiskInfo();
- disk1.setName("D:\\");
- disk1.setTotleSize();
- disk1.setUserSize();
- disk1.setAvilableSize();
- DiskInfo disk2 = new DiskInfo();
- disk2.setName("E:\\");
- disk2.setTotleSize();
- disk2.setUserSize();
- disk2.setAvilableSize();
- ArrayList<DiskInfo> diskInfos = new ArrayList<DiskInfo>();
- diskInfos.add(disk1);
- diskInfos.add(disk2);
- HardwareInfo hwInfo = new HardwareInfo();
- hwInfo.setCpuInfo("cpu info is ...");
- hwInfo.setNetworkInfo("network info is ...");
- hwInfo.setDiskInfos(diskInfos);
- XStream xStream = new XStream();
- xStream.autodetectAnnotations(true);
- String xml = xStream.toXML(hwInfo);
- System.out.println(xml);
- HardwareInfo info2 = (HardwareInfo) xStream.fromXML(xml);
- System.out.println(info2);
- }
- }
package com.ajita.xstream; import java.util.ArrayList; import com.thoughtworks.xstream.XStream; public class TestXStream { /** * @param args */ public static void main(String[] args) { DiskInfo disk1 = new DiskInfo(); disk1.setName("D:\\"); disk1.setTotleSize(100); disk1.setUserSize(20); disk1.setAvilableSize(80); DiskInfo disk2 = new DiskInfo(); disk2.setName("E:\\"); disk2.setTotleSize(200); disk2.setUserSize(40); disk2.setAvilableSize(160); ArrayList<DiskInfo> diskInfos = new ArrayList<DiskInfo>(); diskInfos.add(disk1); diskInfos.add(disk2); HardwareInfo hwInfo = new HardwareInfo(); hwInfo.setCpuInfo("cpu info is ..."); hwInfo.setNetworkInfo("network info is ..."); hwInfo.setDiskInfos(diskInfos); XStream xStream = new XStream(); xStream.autodetectAnnotations(true); String xml = xStream.toXML(hwInfo); System.out.println(xml); HardwareInfo info2 = (HardwareInfo) xStream.fromXML(xml); System.out.println(info2); } }
输出如下:
- <HardwareInfo>
- <cpuInfo>cpu info is ...</cpuInfo>
- <networkInfo>network info is ...</networkInfo>
- <diskInfos>
- <DiskInfo diskName="D:\">
- <totalSize></totalSize>
- <avilableSize></avilableSize>
- <usedSize></usedSize>
- </DiskInfo>
- <DiskInfo diskName="E:\">
- <totalSize></totalSize>
- <avilableSize></avilableSize>
- <usedSize></usedSize>
- </DiskInfo>
- </diskInfos>
- </HardwareInfo>
- cpu info is ...
- network info is ...
- D:\
- TotalSize:|Used Size:|Available Size:
- E:\
- TotalSize:|Used Size:|Available Size:
<HardwareInfo> <cpuInfo>cpu info is ...</cpuInfo> <networkInfo>network info is ...</networkInfo> <diskInfos> <DiskInfo diskName="D:\"> <totalSize>100</totalSize> <avilableSize>80</avilableSize> <usedSize>20</usedSize> </DiskInfo> <DiskInfo diskName="E:\"> <totalSize>200</totalSize> <avilableSize>160</avilableSize> <usedSize>40</usedSize> </DiskInfo> </diskInfos> </HardwareInfo> cpu info is ... network info is ... D:\ TotalSize:100|Used Size:20|Available Size:80 E:\ TotalSize:200|Used Size:40|Available Size:160
4.常用XStream注解
1)@XStreamAlias注解可在类与属性上使用设置名称,相当于: xstream.alias("blog", Blog.class);
2)@XStreamImplicit去集合节点名:相当于 xstream.addImplicitCollection(Blog.class, "entries");
3) @XStreamConverter(SingleValueCalendarConverter.class)
private Calendar createdTime = new GregorianCalendar();
XStream的更多相关文章
- XStream将java对象转换为xml时,对象字段中的下划线“_”,转换后变成了两个的解决办法
在前几天的一个项目中,由于数据库字段的命名原因 其中有两项:一项叫做"市场价格"一项叫做"商店价格" 为了便于区分,遂分别将其命名为market ...
- XStream xml 解析框架使用笔记
1. xml的标签可以映射为类.类成员变量 2. 有子标签的标签映射为类,没有子标签的便签映射为类成员变量 3. 类名.类成员变量名如与标签名不一致需要通过注解或代码设置别名 // 类名 @XStre ...
- Xstream学习资料
java中有关xml操作的,我们项目中首推Xstream.至于原因不说了.跟着大众的脚步走应该没错的.有关Xstream的文档如下. 官方文档 XStream完美转换XML.JSON XStream实 ...
- XStream简单入门
简单的讲,XStream 涉及的就五个知识点:详情参考 官网 混叠,注解,转换器,对象流和操作json! 下面就用几个简单的例子来实现上述五个知识点! 基本步骤: 第1步:创建XStream对象. 通 ...
- 使用XStream解析MXL文件用到的jar包---xpp3_min-1.1.3.4.O.jar和xstream-1.3.1.jar
使用XStream解析MXL文件用到的jar包---xpp3_min-1.1.3.4.O.jar和xstream-1.3.1.jar
- Xstream解析XML
<oschina> <catalog>1</catalog> <newsCount>0</newsCount> <pagesize&g ...
- 打造完美的xml技术解决方案(dom4j/xstream)
转: XML 技术是随着 Java 的发展而发展起来的.在 XML 出现之前对于简单的数据格式通常是存储在 ini 配置文件等文本文件中,复杂的格式则采用自定义的文件格式,因此对于每种文件格式都要有专 ...
- XStream学习笔记
XStream 所需jar包: xstream-1.3.jar xpp3_min-1.1.4c.jar xmlpull-1.1.3.1.jar 目录: 1.注解去除,标签中带有包名的节点 2.注解修改 ...
- Java对象表示方式2:XStream实现对对象的XML化
上一篇文章讲到了使用Java原生的序列化的方式来表示一个对象.总结一下这种对象表示方式的优缺点: 1.纯粹的Java环境下这种方式可以很好地工作,因为它是Java自带的,也不需要第三方的Jar包的支持 ...
- XStream、JAXB 日期(Date)、数字(Number)格式化输出xml
XStream.Jaxb是java中用于对象xml序列化/反序列化 的经典开源项目,利用它们将对象转换成xml时,经常会遇到日期(Date).数字按指定格式输出的需求,下面是使用示例: 一.日期字段格 ...
随机推荐
- DS4700电池更换步骤
DS4700电池更换步骤: 在A控制器里操作(带电热插拔控制器)对逻辑盘进行切换: (需要先将A控下挂的硬盘手工切换到B控上.然后对A控进行电池更换,更换完成后再将原A控下挂硬盘切回) 如下详细步骤: ...
- mysql获取某个表的所有字段名
http://www.netingcn.com/mysql-column-name.html mysql安装成功后可以看到已经存在mysql.information_schema和test这个几个数据 ...
- Java获取随机数的3种方法
最小值---最大值(整数)的随机数 方法1 (数据类型)(最小值+Math.random()*(最大值-最小值+1)) 例: (int)(1+Math.random()*(10-1+1)) / ...
- Nginx+tomcat配置集群负载均衡
开发的应用采用F5负载均衡交换机,F5将请求转发给5台hp unix服务器,每台服务器有多个webserver实例,对外提供web服务和socket等接口服务.之初,曾有个小小的疑问为何不采用开源的a ...
- My Stuck in C++
My Stuck in C++ Zhong-Liang Xiang Oct. 1st, 2017 这个专题记录了对于我而言, c++迷一样的东西.
- 微信小程序--试水
应公司需求,接手小程序,在此之前我是一点也没有接触过,对此,拿过小程序文档和官方案例就一顿恶补,在此期间也看过一些小程序建立模型的视频,终于对小程序知晓一二,拿过项目开始研究.好了废话不多说,总结一下 ...
- PHP While 循环
PHP 循环 - While 循环 循环执行代码块指定的次数,或者当指定的条件为真时循环执行代码块. PHP 循环 在您编写代码时,您经常需要让相同的代码块一次又一次地重复运行.我们可以在代码中使用循 ...
- B+树在磁盘存储中的应用
欢迎探讨,如有错误敬请指正 如需转载,请注明出处 http://www.cnblogs.com/nullzx/ 我们首先提一个问题, B+树比平衡二叉树在索引数据方面要快么? 大多数人可能认为肯定还是 ...
- Redis Cluster架构优化
Redis Cluster架构优化 在<全面剖析Redis Cluster原理和应用>中,我们已经详细剖析了现阶段Redis Cluster的缺点: 无中心化架构 Gossip消息的开销 ...
- nginx+tomcat负载均衡和session复制
本文介绍下传统的tomcat负载均衡和session复制. session复制是基于JVM内存的,当然在当今的互联网大数据时代,有更好的替代方案,如将session数据保存在Redis中. 1.安装n ...