XStream簡介

蘇保嘉 2019/12/27 13:20:28
69

一、前言

XStream是一個Java通用library,可以將Java物件轉換成XML,其優點為靈活易用、高性能、整潔的XML

本篇文章將透過簡單的實作來簡介XStream的使用

 

 

二、實作步驟

1.建立完SpringBoot Maven專案後,在pom.xml導入XStream library

<dependency>
  <groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>

 

 

2.建立User.class

public class User {
    private String userName;
    private String email;

    public User() {
    }

    public User(String userName, String email) {
        this.userName = userName;
        this.email = email;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

 

 

3.XStream中可以直接呼叫方法來將JavaBean轉換成XML

		List<User> userList = new ArrayList();
		User user = new User("User1","User1@gmail.com");
		User user2 = new User("User2","User2@gmail.com");
		User user3 = new User("User3","User3@gmail.com");
		userList.add(user);
		userList.add(user2);
		userList.add(user3);

		//Object to XML
		XStream xStream = new XStream();
		xStream.alias("User", User.class);
		String xml = xStream.toXML(userList);
		System.out.println(xml);

輸出結果:

 

 

4.XStream也可以使用aliasField()來替節點取別名

		List<User> userList = new ArrayList();
		User user = new User("User1","User1@gmail.com");
		User user2 = new User("User2","User2@gmail.com");
		userList.add(user);
		userList.add(user2);

		//Object to XML
		XStream xStream = new XStream();
		xStream.alias("User", User.class);
		//對節點取別名
		xStream.aliasField("E-Mail", User.class,"email");
		String xml = xStream.toXML(userList);
		System.out.println(xml);

輸出結果:

可以看到原本email的名稱在使用aliasField()被我們改變了

 

 

5.我們也可以使用Annotation來將JavaBean轉換成xml

package com.example.demo;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("user")
public class User {

    @XStreamAlias("username")
    private String userName;

    @XStreamAlias("email")
    private String email;

    public User() {
    }

    public User(String userName, String email) {
        this.userName = userName;
        this.email = email;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
		List<User> userList = new ArrayList();
		User user = new User("User1","User1@gmail.com");
		User user2 = new User("User2","User2@gmail.com");
		userList.add(user);
		userList.add(user2);

		//Object to XML
		XStream xStream = new XStream();
		//XStream預設不會讀取Annotation,需要使用此方法讓XStream處理Annotation
		xStream.processAnnotations(User.class);
		String xml = xStream.toXML(userList);
		System.out.println(xml);

輸出結果:

比對最一開始ˋ直接呼叫方法的輸出結果可以發現節點的名稱變成剛剛Annotation所宣告的名稱

 

 

6.最後來試試將XML轉換成java物件

		//XML to Java
		XStream xStream = new XStream();
		xStream.alias("User", User.class);
		String xml = "<User>\n" +
					 "  <userName>test1</userName>\n" +
					 "  <email>test1@gmail.com</email>\n" +
					 "</User>";
		User user = (User)xStream.fromXML(xml);
		System.out.println("userName: "+user.getUserName());
		System.out.println("email: "+user.getEmail());

輸出結果:

 

 

以上這些都是比較基礎的實作呈現XStream還有許多方便的API可以使用,另外Xstream預設在String為null時不封裝到XML中,可視情況將值設成Null

或空字串

 

參考網站: https://x-stream.github.io/tutorial.html

 

 

 

蘇保嘉