简述
Mooasing 是一种自定义生成的 XML 或使用 XStream 使用特定格式的 XML 的技术。假设以下 XML 格式将用于序列化/反序列化 Student 对象。
<student name = "Suresh">
<note>
<title>first</title>
<description>My first assignment.</description>
</note>
<note>
<title>second</title>
<description>My second assignment.</description>
</note>
</student>
基于上述 XML 格式,让我们创建模型类。
class Student {
private String studentName;
private List<Note> notes = new ArrayList<Note>();
public Student(String name) {
this.studentName = name;
}
public void addNote(Note note) {
notes.add(note);
}
public String getName() {
return studentName;
}
public List<Note> 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 测试上述对象序列化。
在 C:\>XStream_WORKSPACE\com\jc2182\xstream 中创建一个名为 XStreamTester 的 java 类文件。
File: XStreamTester.java
package com.jc2182.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<Note> notes = new ArrayList<Note>();
public Student(String name) {
this.studentName = name;
}
public void addNote(Note note) {
notes.add(note);
}
public String getName() {
return studentName;
}
public List<Note> 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;
}
}
Verify the Result
使用编译类 javac 编译器如下 -
C:\XStream_WORKSPACE\com\jc2182\xstream>javac XStreamTester.java
现在运行 XStreamTester 以查看结果 -
C:\XStream_WORKSPACE\com\jc2182\xstream>java XStreamTester
验证输出如下 -
<?xml version = "1.0" encoding = "UTF-8"?>
<com.jc2182.xstream.Student>
<studentName>Mahesh</studentName>
<notes>
<com.jc2182.xstream.Note>
<title>first</title>
<description>My first assignment.</description>
</com.jc2182.xstream.Note>
<com.jc2182.xstream.Note>
<title>second</title>
<description>My Second assignment.</description>
</com.jc2182.xstream.Note>
</notes>
</com.jc2182.xstream.Student>