1 package org.zhb.test; 2 /** 3 * author : zhb 4 * data : 2014-2-14 5 * use packages: 6 * xmlpull-1.1.3.1.jar 7 * xpp3_min-1.1.4c.jar 8 * xstream-1.4.7.jar 9 */ 10 import java.io.File; 11 import java.io.FileInputStream; 12 import java.io.FileOutputStream; 13 import java.io.FileWriter; 14 import java.io.InputStreamReader; 15 import java.io.OutputStreamWriter; 16 17 import com.thoughtworks.xstream.XStream; 18 19 class Person{ 20 private String name; 21 private int age; 22 private String gender; 23 private Dog dog; 24 public String getName() { 25 return name; 26 } 27 public void setName(String name) { 28 this.name = name; 29 } 30 public Person(){}; 31 public Person(String name, int age, String gender, Dog dog) { 32 this.name = name; 33 this.age = age; 34 this.gender = gender; 35 this.dog = dog; 36 } 37 public int getAge() { 38 return age; 39 } 40 public void setAge(int age) { 41 this.age = age; 42 } 43 public String getGender() { 44 return gender; 45 } 46 public void setGender(String gender) { 47 this.gender = gender; 48 } 49 public Dog getDog() { 50 return dog; 51 } 52 public void setDog(Dog dog) { 53 this.dog = dog; 54 } 55 public String toString(){ 56 return "perName: " + this.name + " " + 57 "perAge: " + this.age + " " + 58 "perGender: " + this.gender + " " + 59 "dogName: " + this.dog.getName() + " " + 60 "dogAge: " + this.dog.getAge() + " " + 61 "dogGender: " + this.dog.getGender(); 62 } 63 } 64 class Dog{ 65 private String name; 66 private int age; 67 private String gender; 68 public Dog(){}; 69 public Dog(String name, int age, String gender) { 70 super(); 71 this.name = name; 72 this.age = age; 73 this.gender = gender; 74 } 75 public String getName() { 76 return name; 77 } 78 public void setName(String name) { 79 this.name = name; 80 } 81 public int getAge() { 82 return age; 83 } 84 public void setAge(int age) { 85 this.age = age; 86 } 87 public String getGender() { 88 return gender; 89 } 90 public void setGender(String gender) { 91 this.gender = gender; 92 } 93 } 94 public class TestXS { 95 private static XStream xs; 96 static{ 97 xs = new XStream(); 98 } 99 public static void main(String[] args) throws Exception { 100 Person per = new Person("王大牛", 29, "男", new Dog("李二狗", 5, "男")); 101 // String xmlResult = xs.toXML(per); 102 // System.out.println(xmlResult);//控制台输出 103 String pathName = "D:/per.xml"; 104 // xml2File(per, pathName, "gbk", "Sola"); 105 Person per2 = (Person)File2Object(pathName, per, "Sola", "gbk"); 106 System.out.println(per2); 107 108 } 109 public static void xml2File(Object obj, String pathName, String aliasName) throws Exception{ 110 //按系统默认编码写到文件 111 if(null != aliasName && !("".equals(aliasName)))//不使用别名此参数填入""或null 112 xs.alias(aliasName, obj.getClass());//为最外层标签指定别名,默认为包名+类名 113 xs.toXML(obj, new FileWriter(new File(pathName))); 114 //或 xs.toXML(obj, new FileOutputStream(new File(pathName))); 115 } 116 public static void xml2File(Object obj, String pathName, String charsetName, String aliasName) throws Exception{ 117 //按指定编码写到文件 118 if(null != aliasName && !("".equals(aliasName)))//不使用别名此参数填入""或null 119 xs.alias(aliasName, obj.getClass());//为最外层标签指定别名,默认为包名+类名 120 xs.toXML(obj, new OutputStreamWriter(new FileOutputStream(new File(pathName)), charsetName)); 121 } 122 public static Object File2Object(String pathName, Object obj, String rootName){ 123 //按系统默认编码读取文件 124 if(null != obj && null != rootName)//未使用别名,两个参数填入null即可 125 xs.aliasType(rootName, obj.getClass());//写入文件时用过别名,读取时许指定别名对应的类 126 return xs.fromXML(new File(pathName)); 127 } 128 public static Object File2Object(String pathName, Object obj, String rootName, String charsetName) throws Exception{ 129 //按指定编码读取文件 130 if(null != obj && null != rootName)//未使用别名,两个参数填入null即可 131 xs.aliasType(rootName, obj.getClass());//写入文件时用过别名,读取时许指定别名对应的类 132 return xs.fromXML(new InputStreamReader(new FileInputStream(new File(pathName)), charsetName)); 133 } 134 }