XStream - 别名
别名是用来将Java对象序列化为特殊格式的XML的一种方式,接下来我们看一下将Student对象格式化成指定格式XML的例子。
firstMy first assignment.
secondMy second assignment.
基于上面的XML格式,创建一个Studment类.class Student {
private String studentName;
private List notes = new ArrayList();
public Student(String name) {
this.studentName = name;
}
public void addNote(Note note) {
notes.add(note);
}
public String getName(){
return studentName;
}
public List getNotes(){
return notes;
}
}
class Note {
private String title;
private String description;
public Note(String title, String description) {
this.title = title;
this.description = description;
}
public String getTitle(){
return title;
}
public String getDescription(){
return description;
}
}
开始测试用XStream将Student序列化化为XML.
创建类:XStreamTester 于 C:\>XStream_WORKSPACE\com\tutorialspoint\xstream.
文件名: XStreamTester.javapackage com.tutorialspoint.xstream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.InputSource;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;
public class XStreamTester {
public static void main(String args[]){
XStreamTester tester = new XStreamTester();
XStream xstream = new XStream(new StaxDriver());
Student student = tester.getStudentDetails();
//Object to XML Conversion
String xml = xstream.toXML(student);
System.out.println(formatXml(xml));
}
private Student getStudentDetails(){
Student student = new Student("Mahesh");
student.addNote(new Note("first","My first assignment."));
student.addNote(new Note("second","My Second assignment."));
return student;
}
public static String formatXml(String xml){
try{
Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
StreamResult res = new StreamResult(new ByteArrayOutputStream());
serializer.transform(xmlSource, res);
return new String(((ByteArrayOutputStream)res.getOutputStream()).toByteArray());
}catch(Exception e){
return xml;
}
}
}
class Student {
private String studentName;
private List notes = new ArrayList();
public Student(String name) {
this.studentName = name;
}
public void addNote(Note note) {
notes.add(note);
}
public String getName(){
return studentName;
}
public List getNotes(){
return notes;
}
}
class Note {
private String title;
private String description;
public Note(String title, String description) {
this.title = title;
this.description = description;
}
public String getTitle(){
return title;
}
public String getDescription(){
return description;
}
}
验证结果
javac编译:C:\XStream_WORKSPACE\com\tutorialspoint\xstream>javac XStreamTester.java
运行XStreamTester:C:\XStream_WORKSPACE\com\tutorialspoint\xstream>java XStreamTester
验证程序输出:<?xml version="1.0" encoding="UTF-8"?>
Mahesh
firstMy first assignment.
secondMy Second assignment.
在上述输出中,Student对象被序列化为完全路径XML标签