XStream包混疊


包裝混疊用於建立一個類XML的完全限定名稱的別名到一個新的限定名稱。再次修改我們的例子中並更改以下程式碼。

xstream.alias("student", Student.class);
xstream.alias("note", Note.class);
修改為:
xstream.aliasPackage("my.company.xstream", "com.yiibai.xstream");

讓我們使用XStream測試上述物件序列化。

建立一個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 XStreamTester();
      XStream xstream = new XStream(new StaxDriver());
      xstream.alias("student", Student.class);
      xstream.alias("note", Note.class);
      xstream.useAttributeFor(Student.class, "studentName");
      xstream.aliasField("name", Student.class, "studentName");
      xstream.addImplicitCollection(Student.class, "notes");
      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;
   }     
}

驗證結果

使用javac編譯器編譯如下類:

C:\XStream_WORKSPACE\com\yiibai\xstream>javac XStreamTester.java

現在執行XStreamTester看到的結果:

C:\XStream_WORKSPACE\com\yiibai\xstream>java XStreamTester

驗證輸出

<?xml version="1.0" encoding="UTF-8"?>
<my.company.xstream.Student name="Mahesh">
  <my.company.xstream.Note>
    <title>first</title>
    <description>My first assignment.</description>
  </my.company.xstream.Note>
  <my.company.xstream.Note>
    <title>second</title>
    <description>My Second assignment.</description>
  </my.company.xstream.Note>
</my.company.xstream.Student>