XStream是個很強大的工具,能將Java對象和xml之間相互轉化。xstream不在乎java類中成員變量是私有仍是公有,也不在意是否有默認構造函數。它調用方式也很是簡單:從xml對象轉化爲java對象,使用fromXML()方法;從java對象序列化爲xml,toXML()便可,很方便。xstream也支持註解方式,這些都是爲了簡化輸出而設計,下面爲你們簡單說一下這幾個註解的含義和用法。java
1. 當沒有任何註解狀況下node
- 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;
- }
-
其中Ball定義以下:函數
- public class Ball {
-
- private String color;
-
- Ball(String color){
- this.color = color;
- }
沒有任何註解,輸出以下:工具
- public static void main(String[] args) throws Exception{
-
- List<Ball> balls = new ArrayList<Ball>();
- balls.add(new Ball("red"));
- Cat cat = new Cat("餛飩",1,balls);
-
-
- 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); this
- @XStreamAlias("cat")
- public class Cat {
-
- private String name;
-
- private Integer age;
-
- private List<Ball> balls;
咱們須要明確給出,哪一個類的註解須要被激活:spa
- public static void main(String[] args) throws Exception{
-
- List<Ball> balls = new ArrayList<Ball>();
- balls.add(new Ball("red"));
- Cat cat = new Cat("餛飩",1,balls);
-
-
- FileOutputStream fout = new FileOutputStream("D:/cat.xml");
- XStream xs = new XStream();
-
- xs.processAnnotations(Cat.class);
- xs.toXML(cat,fout);
- }
當咱們在Cat類名使用該註解時,代表該類序列化爲xml時,類名com.timejob.node.Cat將替換成cat輸出,這樣使得xml更清晰簡短:.net
- <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
- private String name;
-
- private Integer age;
-
- private List<Ball> balls;
-
其餘代碼保持不變,輸出後cat.xml以下: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")
- 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
- 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
- private String noCare;
經常使用的就這麼幾個,其餘的再討論和學習吧。