轉自:http://blog.csdn.net/robert_mm/article/details/8459879
XStream是個很強大的工具,能將java對象和xml之間相互轉化。xstream不在意java類中成員變量是私有還是公有,也不在乎是否有默認構造函數。它調用方式也非常簡單:從xml對象轉化為java對象,使用fromXML()方法;從java對象序列化為xml,toXML()即可,很方便。xstream也支持注解方式,這些都是為了簡化輸出而設計,下面為大家簡單說一下這幾個注解的含義和用法。
1. 當沒有任何注解情況下
- public class Cat {
- //名字
- private String name;
- //大小
- private Integer age;
- //玩具球 球具有顏色屬性
- private List<Ball> balls;
- Cat(String name,Integer age,List<Ball> balls){
- this.name = name;
- this.age = age;
- this.balls=balls;
- }
- //getter/setter方法 為了賦值使用
其中Ball定義如下:
- public class Ball {
- //顏色
- private String color;
- Ball(String color){
- this.color = color;
- }
沒有任何注解,輸出如下:
- public static void main(String[] args) throws Exception{
- //初始化cat
- List<Ball> balls = new ArrayList<Ball>();
- balls.add(new Ball("red"));
- Cat cat = new Cat("餛飩",1,balls);
- //初始化結束
- //為了方便查找 將文件制定到D盤cat.xml中
- FileOutputStream fout = new FileOutputStream("D:/cat.xml");
- XStream xs = new XStream();
- xs.toXML(cat,fout);
- }<span style="font-size:14px;">
- </span>
得到Cat.xml如下
- <com.timejob.node.Cat>
- <name>餛飩</name>
- <age>1</age>
- <balls>
- <com.timejob.node.Ball>
- <color>red</color>
- </com.timejob.node.Ball>
- </balls>
- </com.timejob.node.Cat>
1. @XStreamAlias("cat") 等同於 xstream.alias("cat", Cat.class);
- @XStreamAlias("cat") //here
- public class Cat {
- //名字
- private String name;
- //大小
- private Integer age;
- //玩具球 球具有顏色屬性
- private List<Ball> balls;
我們需要明確給出,哪個類的注解需要被激活:
- public static void main(String[] args) throws Exception{
- //初始化cat
- List<Ball> balls = new ArrayList<Ball>();
- balls.add(new Ball("red"));
- Cat cat = new Cat("餛飩",1,balls);
- //初始化結束
- //為了方便查找 將文件制定到D盤cat.xml中
- FileOutputStream fout = new FileOutputStream("D:/cat.xml");
- XStream xs = new XStream();
- //xs.alias("cat", Cat.class); //等同於 @XStreamAlias("cat")
- xs.processAnnotations(Cat.class);//將Cat.class類上的注解將會使用
- xs.toXML(cat,fout);
- }
當我們在Cat類名使用該注解時,表明該類序列化為xml時,類名com.timejob.node.Cat將替換成cat輸出,這樣使得xml更清晰簡短:
- <cat>
- <name>餛飩</name>
- <age>1</age>
- <balls>
- <com.timejob.node.Ball>
- <color>red</color>
- </com.timejob.node.Ball>
- </balls>
- </cat>
2. XStreamAsAttribute 作用是將類內成員作為父節點屬性輸出,等同於xstream.useAttributeFor(Cat.class, "name")
- @XStreamAlias("cat")
- public class Cat {
- //名字
- @XStreamAsAttribute // here 將name作為Cat屬性輸出在父節點
- private String name;
- //大小
- private Integer age;
- //玩具球 球具有顏色屬性
- private List<Ball> balls;
其他代碼保持不變,輸出后cat.xml如下:
- <cat name="餛飩">
- <age>1</age>
- <balls>
- <com.timejob.node.Ball>
- <color>red</color>
- </com.timejob.node.Ball>
- </balls>
- </cat>
我們看到name屬性已經作為 cat的屬性輸出在根節點上
3. @XStreamAlias 作用就是將屬性按照別名輸出,等同於xstream.aliasField("catAge", Cat.class, "age");
- @XStreamAlias("cat")
- public class Cat {
- //名字
- @XStreamAsAttribute
- private String name;
- //大小
- @XStreamAlias("catAge") //here
- private Integer age;
- //玩具球 球具有顏色屬性
- private List<Ball> balls;
得到cat.xml文件如下:
- <cat name="餛飩">
- <catAge>1</catAge>
- <balls>
- <com.timejob.node.Ball>
- <color>red</color>
- </com.timejob.node.Ball>
- </balls>
- </cat>
4.對於集合常用的注解 @XStreamImplicit 去除<Balls></Balls>顯示,只顯示之間的<Ball></Ball>節點元素
- @XStreamAlias("cat")
- public class Cat {
- //名字
- @XStreamAsAttribute
- private String name;
- //大小
- @XStreamAlias("catAge")
- private Integer age;
- //玩具球 球具有顏色屬性
- @XStreamImplicit //here
- private List<Ball> balls;
此時輸出如下:
- <cat name="餛飩">
- <catAge>1</catAge>
- <com.timejob.node.Ball>
- <color>red</color>
- </com.timejob.node.Ball>
- </cat>
這樣看起來就更加簡潔了。
4.還有幾個注解,都是比較好理解的,如@XStreamOmitField 表明該屬性不會被序列化到xml中。
- @XStreamAlias("cat")
- public class Cat {
- //名字
- @XStreamAsAttribute
- private String name;
- //大小
- @XStreamAlias("catAge")
- private Integer age;
- //玩具球 球具有顏色屬性
- @XStreamImplicit
- private List<Ball> balls;
- @XStreamOmitField //here
- 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類上的注解將會使用
這句一定要加上,這句才能使注解生效。