XStream 可以方便地把java对象序列化为XML,和把XML反序列化为java对象。
引入XStream的包:
<!-- XStream --> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.9</version> </dependency>
实体类:
package cn.framework.t1; import java.util.ArrayList; import java.util.Date; import java.util.List; public class User { private int userId; private String userName; private String pwd; private List<UserLog> logs=new ArrayList<UserLog>(); private Date lastVisted; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public List getLogs() { return logs; } public void setLogs(List logs) { this.logs = logs; } public Date getLastVisted() { return lastVisted; } public void setLastVisted(Date lastVisted) { this.lastVisted = lastVisted; } @Override public String toString() { return "User [userId=" + userId + ",userName=" + userName + ",pwd=" + pwd + ",logs=" + logs + ",lastVisted=" + lastVisted + "]"; } }
package cn.framework.t1; public class UserLog { private int logId; private String ip; public int getLogId() { return logId; } public void setLogId(int logId) { this.logId = logId; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } @Override public String toString() { return "UserLog [logId=" + logId + ",ip=" + ip + "]"; } }
XStreamUtils
package cn.framework.t1; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import org.apache.log4j.Logger; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; public class XStreamUtils { private static final Logger logger=Logger.getLogger("cn.framework.t1.XStreamUtils"); private static XStream xstream; static{ logger.info("init xstream ..."); xstream=new XStream(new DomDriver()); /*******对生成的xml做一些设置*******/ //设置别名,不设置将采用完全类名 xstream.alias("User",User.class); xstream.alias("UserLog",UserLog.class); //设置类成员别名 xstream.aliasField("id",User.class,"userId"); //类的属性默认为xml的元素,现在修改为xml属性 xstream.aliasAttribute(UserLog.class,"logId","lid"); xstream.useAttributeFor(UserLog.class,"logId"); //只保留集合内元素,去掉上级标签 xstream.addImplicitCollection(User.class,"logs"); } public static void objectToXML(User user) throws FileNotFoundException{ logger.info("do objectToXML ..."); FileOutputStream fileOutputStream = new FileOutputStream("e:/out/xstream_user.xml"); xstream.toXML(user,fileOutputStream); } public static void xmlToObject() throws FileNotFoundException{ logger.info("do xmlToObject ..."); FileInputStream input = new FileInputStream("e:/out/xstream_user.xml"); User user=(User) xstream.fromXML(input); System.out.println(user); } }
测试类:
package cn.framework.t1; import java.io.FileNotFoundException; import java.util.Date; public class Test { public static void main(String[] args) throws FileNotFoundException { User user = new User(); user.setLastVisted(new Date()); user.setPwd("123456"); user.setUserId(1); user.setUserName("aaa"); UserLog userLog = new UserLog(); userLog.setIp("127.0.0.1"); userLog.setLogId(1); user.getLogs().add(userLog); UserLog userLog1 = new UserLog(); userLog1.setIp("127.0.0.1"); userLog1.setLogId(2); user.getLogs().add(userLog1); XStreamUtils.objectToXML(user); // XStreamUtils.xmlToObject(); } }
默认生成的xml格式:
<cn.framework.t1.User> <userId>1</userId> <userName>aaa</userName> <pwd>123456</pwd> <logs> <cn.framework.t1.UserLog> <logId>1</logId> <ip>127.0.0.1</ip> </cn.framework.t1.UserLog> <cn.framework.t1.UserLog> <logId>2</logId> <ip>127.0.0.1</ip> </cn.framework.t1.UserLog> </logs> <lastVisted>2017-12-06 03:23:50.11 UTC</lastVisted> </cn.framework.t1.User>
对生成的xml做设置后 生成的xml格式:
<User> <id>1</id> <userName>aaa</userName> <pwd>123456</pwd> <UserLog lid="1"> <ip>127.0.0.1</ip> </UserLog> <UserLog lid="2"> <ip>127.0.0.1</ip> </UserLog> <lastVisted>2017-12-06 03:25:00.193 UTC</lastVisted> </User>