---
title: "xstream"
author: "floydd"
date: "2007-06-30T12:16:00Z"
tags: []
slug: "xstream"
draft: false
lastmod: "2011-12-26T12:03:30Z"
id: 1326398
url: "https://floydd.iteye.com/blog/1326398"
---

有很多xml 和 java object 的binding tools,最近看到xstream这个名词，然后又有一个同事正好在用，所以看了一下，发现和castor很类似(虽然官网说it's not a data binding tools but a serialization tool,我觉得还是很象的)——默认不配置的情况下使用都很简单方便。  
(i)首先在默认不配置的情况下  
1\. object -&gt; xml：castor的命名方式似乎根好一点,而且很不喜欢xstream没有一个默认获取alias的规则--总是输出类全名，很麻烦。  
2\. xml -&gt; object：如果你将上面的输出文件修改一下的话（比如元素改为属性） ,castor还能识别，xstream就不行。

(ii) 配置  
如果你双向的绑定(可逆的转换)，建议你用xstream的annotation和castor的mapping.xml 这两种方式，2种实现都是不错的。配置简单明了.

(iii) 序列化手段  
castor是通过get,set以及public 成员来获取属性的;但是xstream直接获取成员变量（包括private的）

此外，xstream 还提供了多个对象序列化到一个文件的功能（java序列化实现），以及序列化一组对象到N个文件的集合方法；支持json序列化和反序列化；支持annotation的配置，这个还是很不错的,强烈建议总是使用这种方式来使用xstream .

xstream总体上给人比较新而轻的感觉:) -- 一直以来印象中castor是很小巧的,这次一看1.1.2.1-xml.jar有1MB ...汗

ps:

1\. 好久不用 castor竟然mapping一开始都没正确读取-- 使用mapping时,不能调2个参数的那个静态方法,否则mapping 无效.

2\. 通过反射竟然可以获取private 属性...以前以为不可以(如果设置了安全策略应该没有这么简单),获取方法如下:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassTest...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateStringstr="csdn";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)privatevoidprint()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.out.println("str="+str);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidmain(String\[]args)throwsException...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Testt=newTest();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)t.print();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Field\[]f=t.getClass().getDeclaredFields();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(inti=0;i&lt;f.length;i++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)f\[i].setAccessible(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//System.out.println(f\[i].getType());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)f\[i].set(t,"floyd");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)t.print();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}