XStream混疊


混疊是一種技術來客製化生成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 測試上述物件序列化。

建立一個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());
      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"?>
<com.yiibai.xstream.Student>
  <studentName>Mahesh</studentName>
  <notes>
    <com.yiibai.xstream.Note>
      <title>first</title>
      <description>My first assignment.</description>
    </com.yiibai.xstream.Note>
    <com.yiibai.xstream.Note>
      <title>second</title>
      <description>My Second assignment.</description>
    </com.yiibai.xstream.Note>
  </notes>
</com.yiibai.xstream.Student>

在上面的結果,我們已經看到了Student物件名稱是完全合格的。要替換它作為學生的標籤,按照下面的連結。

類混疊

另外,在上述結果中可以看出,所需studentName要重新命名來命名。要取代它,按照下面的連結。

欄位混疊

在上面的結果,我們可以看到,筆記標記被新增成為筆記列表。替換它,按照下面的連結。

隱式集合混疊

在上面的結果,我們可以看到這個名字來作為一個子節點,需要將它作為根節點的屬性。替換它,按照下面的連結。

屬性混疊

包混疊