XStream主要用来做Java对象和XML 之间的转换器,转换过程是双向的。核心类就是XStream 类,下面就来简单介绍一下XStream最常用,也是最简单的使用
主要有两个方法
toXML()方法。//将bean转换为xml
fromXML()方法。//将xml转换为bean
默认情况下,java 到xml 的映 射,是java 成员名对应xml 的元素名,java 类的全名对
应xml 根元素的名字。而实际中,往往是xml 和java 类都有了,要完成相互转换,必须进行别名映射。
别名配置包含三种情况:
1、类别名,用alias(String name, Class type)。
2、 类成员别名,用aliasField(String alias, Class definedIn, String fieldName)
3、 类成员作为属性别名,用 aliasAttribute(Class definedIn, String attributeName, String alias),
好了,现在上代码;
1、 定义Page类
@XStreamAlias("Page")
public class Page {
public AddressBook addressBook;
public AddressBook getAddressBook() {
return addressBook;
}
public void setAddressBook(AddressBook addressBook) {
this.addressBook = addressBook;
}
}
2、 定义AddressBook类
@XStreamAlias("AddressBook")
public class AddressBook {
private List<com.xtream.Person> persons;
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
}
3、 定义Person类
@XStreamAlias("Person")
public class Person {
@XStreamAsAttribute
public String name;
@XStreamAsAttribute
public String age;
@XStreamAsAttribute
public String sex;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
4、测试入口
public class Main {
public static void main(String[] args) {
System.out.println("-----------------写xml文件-------------------");
List list = new ArrayList();
for (int i = 0; i < 10; i++) {
Person address = new Person();
address.setName("张三");
address.setAge("26");
address.setSex("男");
list.add(address);
}
AddressBook addes = new AddressBook();
addes.setPersons(list);
Page page = new Page();
page.setAddressBook(addes);
Util.makeXML(page);
Util.writeXML(Util.makeXML(page));
System.out.println("-----------------xml文件内容-------------------");
System.out.println(Util.makeXML(page));
System.out.println("-----------------读xml文件(1) dom4j解析-------------------");
File f = new File("d://testXtram.xml");
try {
InputStream in = new FileInputStream(f);
Page page1 = Util.readXML(in);
for (int i = 0; i < page1.getAddressBook().getPersons().size(); i++) {
Person person = page1.getAddressBook().getPersons().get(i);
System.out.println(person.getName() + " " + person.getAge() + " " + person.getSex());
}
} catch (FileNotFoundException e) {
e.printStackTrace(); }
System.out.println("-----------------读xml文件(2) xstream转换-------------------");
File f2 = new File("d://testXtram.xml");
try {
InputStream in = new FileInputStream(f2);
Page page2 = Util.readXML2(in);
for (int i = 0; i < page2.getAddressBook().getPersons().size(); i++) {
Person person = page2.getAddressBook().getPersons().get(i);
System.out.println(person.getName() + " " + person.getAge() + " " + person.getSex());
}
} catch (FileNotFoundException e) {
e.printStackTrace(); }
}
}
5、public class Util {
public static String makeXML(Page page) {
XStream stream = new XStream();
stream = setXStream(stream);
String str = stream.toXML(page);
return str;
}
public static void writeXML(String s) {
FileWriter fw = null;
try {
fw = new FileWriter("d://testXtram.xml");
fw.write(s, 0, s.length());
fw.flush();
System.out.println("写文件成功!");
} catch (IOException e) {
e.printStackTrace(); }
}
public static Page readXML2(InputStream inputStream) {
XStream stream = new XStream(new DomDriver());
stream = setXStream(stream);
return (Page) stream.fromXML(inputStream);
}
private static XStream setXStream(XStream stream) {
stream.alias("Page", Page.class);
stream.alias("Person", Person.class);
stream.aliasAttribute(Person.class, "name", "name");
stream.aliasAttribute(Person.class, "age", "age");
stream.aliasAttribute(Person.class, "sex", "sex");
stream.alias("AddressBook", AddressBook.class);
return stream;
}
public static Page readXML(InputStream inputStream) {
System.out.println("开始读取xml文件并封装为对象!");
String str = getInputStreamToString(inputStream);
Page page = new Page();
AddressBook addressBook = new AddressBook();
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read(new StringReader(str));
Element root = document.getRootElement();
Element root2 = root.element("addressBook").element("persons");
List<Person> persons = new ArrayList<Person>();
for (Iterator item = root2.elementIterator(); item.hasNext();) {
Element element = (Element) item.next();
Person person = new Person();
person.setName(String.valueOf(element.element("name").getText()));
person.setAge(String.valueOf(element.element("age").getText()));
person.setSex(String.valueOf(element.element("sex").getText()));
persons.add(person);
}
addressBook.setPersons(persons);
} catch (DocumentException e) {
e.printStackTrace(); }
page.setAddressBook(addressBook);
return page;
}
public static String getInputStreamToString(InputStream inputStream) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return sb.toString();
}
}
到这里相信都应该明白了怎么用了吧,如有需要代码的话请联系我:QQ:429472944