需要导入的包和项目如下:
User.java代码:
public class User {
private String id;
private String name ;
private String age;
private String pwd;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", pwd=" + pwd + "]";
}
}
Test.java代码
package com.test;
import com.test.domain.User;
import com.thoughtworks.xstream.XStream;
public class Test {
public static void main(String[] args) {
User user = new User();
user.setAge("11");
user.setId("1");
user.setName("zhangsan");
user.setPwd("123456");
XStream xml = new XStream();
String userXml = xml.toXML(user);
System.out.println("object to xml");
System.out.println(userXml);
System.out.println("=====================================");
System.out.println("xml to object");
User u = (User) xml.fromXML(userXml);
System.out.println(u);
}
}
运行结果:
object to xml
<com.test.domain.User>
<id>1</id>
<name>zhangsan</name>
<age>11</age>
<pwd>123456</pwd>
</com.test.domain.User>
=====================================
xml to object
User [id=1, name=zhangsan, age=11, pwd=123456]
这里可以给输出的xml起一个别名:
例如:下免得测试代码:
package com.test;
import com.test.domain.User;
import com.thoughtworks.xstream.XStream;
public class Test {
public static void main(String[] args) {
User user = new User();
user.setAge("11");
user.setId("1");
user.setName("zhangsan");
user.setPwd("123456");
XStream xml = new XStream();
xml.alias("address", User.class);
String userXml = xml.toXML(user);
System.out.println("object to xml");
System.out.println(userXml);
System.out.println("=====================================");
System.out.println("xml to object");
User u = (User) xml.fromXML(userXml);
System.out.println(u);
}
}
运行结果:
object to xml
<address>
<id>1</id>
<name>zhangsan</name>
<age>11</age>
<pwd>123456</pwd>
</address>
=====================================
xml to object
User [id=1, name=zhangsan, age=11, pwd=123456]
这里我们将user这个对象起了一个别名为address,其他的修改也是同理,如何要给属性起别名就用
xml.aliasField("userid", User.class, "id");
这里我们也可以用注解的方式来起别名:
测试代码如下:
User.java代码
package com.test.domain;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("user")
public class User {
@XStreamAlias("userId")
private String id;
private String name ;
private String age;
private String pwd;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", pwd=" + pwd + "]";
}
}
Test.java测试代码如下:
package com.test;
import com.test.domain.User;
import com.thoughtworks.xstream.XStream;
public class Test {
public static void main(String[] args) {
User user = new User();
user.setAge("11");
user.setId("1");
user.setName("zhangsan");
user.setPwd("123456");
XStream xml = new XStream();
xml.processAnnotations(User.class);
String userXml = xml.toXML(user);
System.out.println("object to xml");
System.out.println(userXml);
System.out.println("=====================================");
System.out.println("xml to object");
User u = (User) xml.fromXML(userXml);
System.out.println(u);
}
}
测试结果如下:
object to xml
<user>
<userId>1</userId>
<name>zhangsan</name>
<age>11</age>
<pwd>123456</pwd>
</user>
=====================================
xml to object
User [id=1, name=zhangsan, age=11, pwd=123456]
@XStreamAlias注解可在类与属性上使用设置名称,相当于: xstream.alias("user", User.class);