【XStream】xml和java实体的相互转化

1.pom.xml

<!-- xstream xml和Java对象转化 -->
<dependency>
<groupId>xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>xpp3</groupId>
<artifactId>xpp3</artifactId>
<version>1.1.3.4.O</version>
</dependency>

2.实体TestEntity.java

package com.sxd.test.controller;

import java.util.List;

/**
* 测试类 TestEntity---要转化为xml的类
* @author SXD
*
*/
public class TestEntity { private Integer money;
private String firstStr;
private boolean flag; private List<Team> teamList; public Integer getMoney() {
return money;
} public void setMoney(Integer money) {
this.money = money;
} public String getFirstStr() {
return firstStr;
} public void setFirstStr(String firstStr) {
this.firstStr = firstStr;
} public boolean isFlag() {
return flag;
} public void setFlag(boolean flag) {
this.flag = flag;
} public List<Team> getTeamList() {
return teamList;
} public void setTeamList(List<Team> teamList) {
this.teamList = teamList;
} @Override
public String toString() {
return "TestEntity [money=" + money + ", firstStr=" + firstStr
+ ", flag=" + flag + ", teamList=" + teamList + "]";
} }

3.实体Team.java

package com.sxd.test.controller;

/**
* 测试类 -- Team实体
* @author SXD
*
*/
public class Team { private String name;
private int num;
private String describe;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
} @Override
public String toString() {
return "Team [name=" + name + ", num=" + num + ", describe=" + describe
+ "]";
} }

4.Test.java测试类

package com.sxd.test.controller;

import java.util.ArrayList;
import java.util.List; import com.thoughtworks.xstream.XStream; public class Test { @org.junit.Test
public void testName() throws Exception {
TestEntity obj = getEntity();
XStream stream = new XStream();
String xml = stream.toXML(obj);
System.out.println(xml); TestEntity testEntity = (TestEntity) stream.fromXML(xml);
System.out.println("");
System.out.println(testEntity.toString()); } public TestEntity getEntity(){
TestEntity entity = new TestEntity();
entity.setMoney(30000);
entity.setFlag(true);
entity.setFirstStr("艾欧尼亚"); List<Team> list = new ArrayList<Team>();
Team team1 = new Team();
team1.setName("英雄1");
team1.setNum(2);
team1.setDescribe("射手英雄,远程攻击型英雄"); list.add(team1); Team team2 = new Team();
team2.setName("英雄2");
team2.setNum(3);
team2.setDescribe("坦克英雄,肉盾抗击打型英雄"); list.add(team2); entity.setTeamList(list); return entity; } }

测试结果:

<com.sxd.test.controller.TestEntity>
<money>30000</money>
<firstStr>艾欧尼亚</firstStr>
<flag>true</flag>
<teamList>
<com.sxd.test.controller.Team>
<name>英雄1</name>
<num>2</num>
<describe>射手英雄,远程攻击型英雄</describe>
</com.sxd.test.controller.Team>
<com.sxd.test.controller.Team>
<name>英雄2</name>
<num>3</num>
<describe>坦克英雄,肉盾抗击打型英雄</describe>
</com.sxd.test.controller.Team>
</teamList>
</com.sxd.test.controller.TestEntity> TestEntity [money=30000, firstStr=艾欧尼亚, flag=true, teamList=[Team [name=英雄1, num=2, describe=射手英雄,远程攻击型英雄], Team [name=英雄2, num=3, describe=坦克英雄,肉盾抗击打型英雄]]]
上一篇:spring-data-jpa Repository的基本知识


下一篇:Spring Boot 使用 Dom4j XStream 操作 Xml