XStream中幾個注解的含義和用法


轉自:http://blog.csdn.net/robert_mm/article/details/8459879

XStream是個很強大的工具,能將java對象和xml之間相互轉化。xstream不在意java類中成員變量是私有還是公有,也不在乎是否有默認構造函數。它調用方式也非常簡單:從xml對象轉化為java對象,使用fromXML()方法;從java對象序列化為xml,toXML()即可,很方便。xstream也支持注解方式,這些都是為了簡化輸出而設計,下面為大家簡單說一下這幾個注解的含義和用法。

1. 當沒有任何注解情況下

[java]  view plain  copy
 
  1. public class Cat {    
  2.     //名字    
  3.     private String name;    
  4.     //大小    
  5.     private Integer age;    
  6.     //玩具球  球具有顏色屬性    
  7.     private List<Ball> balls;    
  8.         
  9.     Cat(String name,Integer age,List<Ball> balls){    
  10.         this.name = name;    
  11.         this.age = age;    
  12.         this.balls=balls;    
  13.     }    
  14.     //getter/setter方法 為了賦值使用    

 

其中Ball定義如下:

 

[java]  view plain  copy
 
  1. public class Ball {  
  2.     //顏色  
  3.     private String color;  
  4.   
  5.     Ball(String color){  
  6.         this.color = color;  
  7.     }  

 

沒有任何注解,輸出如下:

 

[java]  view plain  copy
 
  1. public static void main(String[] args) throws Exception{  
  2.         //初始化cat  
  3.         List<Ball> balls = new ArrayList<Ball>();  
  4.         balls.add(new Ball("red"));  
  5.         Cat cat = new Cat("餛飩",1,balls);  
  6.         //初始化結束  
  7.         //為了方便查找 將文件制定到D盤cat.xml中  
  8.         FileOutputStream fout = new FileOutputStream("D:/cat.xml");  
  9.         XStream xs = new XStream();  
  10.         xs.toXML(cat,fout);  
  11.     }<span style="font-size:14px;">  
  12. </span>  

得到Cat.xml如下

 

 

[java]  view plain  copy
 
  1. <com.timejob.node.Cat>  
  2.   <name>餛飩</name>  
  3.   <age>1</age>  
  4.   <balls>  
  5.     <com.timejob.node.Ball>  
  6.       <color>red</color>  
  7.     </com.timejob.node.Ball>  
  8.   </balls>  
  9. </com.timejob.node.Cat>  


1. @XStreamAlias("cat") 等同於 xstream.alias("cat", Cat.class); 

 

 

[java]  view plain  copy
 
  1. @XStreamAlias("cat")  //here   
  2. public class Cat {  
  3.     //名字  
  4.     private String name;  
  5.     //大小  
  6.     private Integer age;  
  7.     //玩具球  球具有顏色屬性  
  8.     private List<Ball> balls;  

 

我們需要明確給出,哪個類的注解需要被激活:

 

[java]  view plain  copy
 
  1. public static void main(String[] args) throws Exception{  
  2.         //初始化cat  
  3.         List<Ball> balls = new ArrayList<Ball>();  
  4.         balls.add(new Ball("red"));  
  5.         Cat cat = new Cat("餛飩",1,balls);  
  6.         //初始化結束  
  7.         //為了方便查找 將文件制定到D盤cat.xml中  
  8.         FileOutputStream fout = new FileOutputStream("D:/cat.xml");  
  9.         XStream xs = new XStream();  
  10.         //xs.alias("cat", Cat.class); //等同於 @XStreamAlias("cat")  
  11.         xs.processAnnotations(Cat.class);//將Cat.class類上的注解將會使用  
  12.         xs.toXML(cat,fout);  
  13.     }  

當我們在Cat類名使用該注解時,表明該類序列化為xml時,類名com.timejob.node.Cat將替換成cat輸出,這樣使得xml更清晰簡短:

 

 

[java]  view plain  copy
 
  1. <cat>  
  2.   <name>餛飩</name>  
  3.   <age>1</age>  
  4.   <balls>  
  5.     <com.timejob.node.Ball>  
  6.       <color>red</color>  
  7.     </com.timejob.node.Ball>  
  8.   </balls>  
  9. </cat>  


2. XStreamAsAttribute 作用是將類內成員作為父節點屬性輸出,等同於xstream.useAttributeFor(Cat.class, "name")

 

 

[java]  view plain  copy
 
  1. @XStreamAlias("cat")  
  2. public class Cat {  
  3.     //名字  
  4.     @XStreamAsAttribute // here  將name作為Cat屬性輸出在父節點  
  5.     private String name;  
  6.     //大小  
  7.     private Integer age;  
  8.     //玩具球  球具有顏色屬性  
  9.     private List<Ball> balls;  
  10.       

其他代碼保持不變,輸出后cat.xml如下:

 

 

[java]  view plain  copy
 
  1. <cat name="餛飩">  
  2.   <age>1</age>  
  3.   <balls>  
  4.     <com.timejob.node.Ball>  
  5.       <color>red</color>  
  6.     </com.timejob.node.Ball>  
  7.   </balls>  
  8. </cat>  

我們看到name屬性已經作為 cat的屬性輸出在根節點上

 

3. @XStreamAlias 作用就是將屬性按照別名輸出,等同於xstream.aliasField("catAge", Cat.class, "age");

 

[java]  view plain  copy
 
  1. @XStreamAlias("cat")  
  2. public class Cat {  
  3.     //名字  
  4.     @XStreamAsAttribute  
  5.     private String name;  
  6.     //大小  
  7.     @XStreamAlias("catAge")  //here  
  8.     private Integer age;  
  9.     //玩具球  球具有顏色屬性  
  10.     private List<Ball> balls;  

得到cat.xml文件如下:

 

 

[java]  view plain  copy
 
  1. <cat name="餛飩">  
  2.   <catAge>1</catAge>  
  3.   <balls>  
  4.     <com.timejob.node.Ball>  
  5.       <color>red</color>  
  6.     </com.timejob.node.Ball>  
  7.   </balls>  
  8. </cat>  

4.對於集合常用的注解 @XStreamImplicit 去除<Balls></Balls>顯示,只顯示之間的<Ball></Ball>節點元素

 

[java]  view plain  copy
 
  1. @XStreamAlias("cat")  
  2. public class Cat {  
  3.     //名字  
  4.     @XStreamAsAttribute  
  5.     private String name;  
  6.     //大小  
  7.     @XStreamAlias("catAge")  
  8.     private Integer age;  
  9.     //玩具球  球具有顏色屬性  
  10.     @XStreamImplicit    //here  
  11.     private List<Ball> balls;  

 

此時輸出如下:

 

[java]  view plain  copy
 
  1. <cat name="餛飩">  
  2.   <catAge>1</catAge>  
  3.   <com.timejob.node.Ball>  
  4.     <color>red</color>  
  5.   </com.timejob.node.Ball>  
  6. </cat>  

這樣看起來就更加簡潔了。

 

4.還有幾個注解,都是比較好理解的,如@XStreamOmitField 表明該屬性不會被序列化到xml中。

 

[java]  view plain  copy
 
  1. @XStreamAlias("cat")  
  2. public class Cat {  
  3.     //名字  
  4.     @XStreamAsAttribute  
  5.     private String name;  
  6.     //大小  
  7.     @XStreamAlias("catAge")  
  8.     private Integer age;  
  9.     //玩具球  球具有顏色屬性  
  10.     @XStreamImplicit  
  11.     private List<Ball> balls;  
  12.     @XStreamOmitField        //here  
  13.     private String noCare;  

常用的就這么幾個,其他的再討論和學習吧。

 

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

以下為原創:

@XStreamImplicit(itemFieldName="ballName")
itemFieldName支持修改list中每個元素的節點的名字,如下:

、、、、、、、、、、、、、、、
import com.thoughtworks.xstream.XStream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
//初始化cat
List<Ball> balls = new ArrayList<Ball>();
balls.add(new Ball("red"));
balls.add(new Ball("blue"));
Cat cat = new Cat("餛飩",1,balls);
//初始化結束
//為了方便查找 將文件制定到D盤cat.xml中
FileOutputStream fout = null;
try {
fout = new FileOutputStream("D:/cat.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
XStream xs = new XStream();
xs.processAnnotations(Cat.class);//將Cat.class類上的注解將會使用
xs.toXML(cat,fout);
}
}

將輸出:

<com.jianwu.boss.domain.Cat>
<name>餛飩</name>
<age>1</age>
<ballName>
<color>red</color>
</ballName>
<ballName>
<color>blue</color>
</ballName>
</com.jianwu.boss.domain.Cat>

 

注意,

xs.processAnnotations(Cat.class);//將Cat.class類上的注解將會使用
這句一定要加上,這句才能使注解生效。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM