通过xStream读取xml文件生成Java对象和Java对象生成xml文件

Student.java

package com.java.bean;

public class Student {
private Integer id;
private String name;
private Integer age;
private String address;
private String tel;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", address=" + address + ", tel=" + tel + "]";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getTel() {
return tel;
}

public void setTel(String tel) {
this.tel = tel;
}
}

测试类XstreamTest.java

package com.java.azzan;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

import org.junit.Test;

import com.java.bean.Student;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XstreamTest {

public static void main(String[] args) {

}

@Test //读取xml文件转对象
public void readXML () {
XStream xstream = new XStream(new DomDriver());
xstream.alias("student", Student.class);
InputStreamReader o= null;
Student student = null;
File file = new File("C:\\Users\\admin\\Desktop\\xstream.xml");
try {
o = new FileReader(file);
student = (Student) xstream.fromXML(o);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if(o!= null){
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

System.out.println("学号:"+student.getId());
System.out.println("姓名:"+student.getName());
System.out.println("年龄:"+student.getAge());
System.out.println("地址:"+student.getAddress());
System.out.println("电话:"+student.getTel());
}

@Test //将对象信息写入xml文件
public void writeXML () {
XStream xstream = new XStream(new DomDriver("utf-8"));
xstream.alias("student", Student.class);

Student student = new Student();
student.setId(2);
student.setName("Azzan");
student.setAge(22);
student.setAddress("sdheze");
student.setTel("18711111111");

String xmlFileName = "student.xml";
File file = new File("C:\\Users\\admin\\Desktop\\"+xmlFileName);
if (!file.exists()) {
try {
file.createNewFile();
System.out.println("文件创建成功!");
} catch (IOException e) {
System.out.println("文件创建失败!");
e.printStackTrace();
}
}

//String xml = xstream.toXML(student);//不会直接生成xml文件,返回的仅仅只是xml文本字符串
//System.out.println(xml);

OutputStream out;
try {
out = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(out,Charset.forName("GBK"));
writer.write("<?xml version=\"1.0\" encoding=\"GBK\"?>\n");
xstream.toXML(student, writer);
out.close();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}

}
}

要读取的xml文件xstream.xml

<?xml version="1.0" encoding="GBK"?>
<student>
<id>1</id>
<name>azzan</name>
<age>22</age>
<address>China</address>
<tel>18711111111</tel>
</student>

生成的xml文件student.xml

<?xml version="1.0" encoding="GBK"?>
<student>
<id>2</id>
<name>Azzan</name>
<age>22</age>
<address>BeiJing</address>
<tel>18711111111</tel>
</student>