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

通过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>

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值