Category.java
public class Category { private int id; private String name; private String remark; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } }
Content.java
public class Content { private int id; private String title; private String content; private String createTime; private String updateTime; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getCreateTime() { return createTime; } public void setCreateTime(String createTime) { this.createTime = createTime; } public String getUpdateTime() { return updateTime; } public void setUpdateTime(String updateTime) { this.updateTime = updateTime; } }
Java对象转换成XML:
package com; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.charset.Charset; import com.model.Content; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; public class Object2XML { static XStream xs = new XStream(new DomDriver()); static String xmlContent; public static void main(String args[]) { Content content = new Content(); content.setId(001); content.setTitle("XStreamTest"); content.setContent("ooxx"); content.setCreateTime("yesterday"); content.setUpdateTime("now"); try { String location = "d:/temp/object2xml.xml"; File file = new File(location); if(!file.exists()||file.isDirectory()){ String[] arry=location.split("/"); File file1=new File(arry[0]+"/"+arry[1]); //创建文件夹后如果后面对文件操作的话会自动创建文件 file1.mkdir(); } FileOutputStream fos = new FileOutputStream(file); //提示:字节流 OutputStreamWriter osw = new OutputStreamWriter(fos, Charset.forName("gbk")); //作用:使字节流转换成字符流 osw是字符流通向字节流的桥梁,使用Charset指定字符编码 //ows调用write()方法会调用转换器 BufferedWriter bw = new BufferedWriter(osw); //提示:为了获得最高效率,把osw包装到bw中,这样做可以避免频繁调用转换器 bw.write("<?xml version=\"1.0\" encoding=\"gbk\" ?>\n"); //提示:在生成的xml前面加上基本信息 xs.alias("content", Content.class); //提示:给类取别名 xs.aliasField("内容", Content.class, "content"); //提示:给属性取别名 xs.toXML(content, bw); bw.close(); //作用:关闭流 osw.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
结果:
<?xml version="1.0" encoding="gbk" ?> <content> <id>1</id> <title>XStreamTest</title> <内容>ooxx</内容> <createTime>yesterday</createTime> <updateTime>now</updateTime> </content>
XML转换成Java对象:
package com; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import com.model.Content; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; public class XML2Object { static XStream xs = new XStream(new DomDriver()); public static void main(String args[]) { File file = new File("d:/temp/object2xml.xml"); // 名字区分大小写! Content content = new Content(); try { FileInputStream fis = new FileInputStream(file); InputStreamReader isw = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isw); xs.alias("content", Content.class);// 生成与解析时代别名要对应!!!! xs.aliasField("内容", Content.class, "content"); xs.fromXML(br, content); br.close(); isw.close(); fis.close(); System.out.println("id=" + content.getId()); System.out.println("title=" + content.getTitle()); System.out.println("content=" + content.getContent()); System.out.println("createTime=" + content.getCreateTime()); System.out.println("updateTime=" + content.getUpdateTime()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
结果:
id=1
title=XStreamTest
content=ooxx
createTime=yesterday
updateTime=now