混疊是一種技術(shù)來定制生成XML或者使用XStream特定的格式化XML。假設(shè),一個(gè)下面的XML格式是用于序列化/反序列化Student對(duì)象。
<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格式,讓我們創(chuàng)建的模型類。
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è)試上述對(duì)象序列化。
創(chuàng)建一個(gè)Java類名為XStreamTester在文件 C:\>XStream_WORKSPACE\com\yiibai\xstream.
File: XStreamTester.java
package com.yiibai.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上一篇:XStream教程下一篇:XStream包混疊