XStream混叠 - 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要重命名来命名。要取代它,按照下面的链接。

字段混叠

在上面的结果,我们可以看到,笔记标记被添加成为笔记列表。替换它,按照下面的链接。

隐式集合混叠

在上面的结果,我们可以看到这个名字来作为一个子节点,需要将它作为根节点的属性。替换它,按照下面的链接。

属性混叠 包混叠