位置:首頁 > Java技術 > XStream教學 > XStream注解

XStream注解

XStream支持注釋做同樣的任務,正如我們在前麵的章節中是自動配置,而不是代碼1所做的。在前麵的章節中,我們已經看到了下麵的代碼配置。

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");

下麵的代碼片段來說明使用annoations更容易的方式做同樣的工作。


@XStreamAlias("student")    //define class level alias
class Student {

    
    @XStreamAlias("name")   //define field level alias
	@XStreamAsAttribute     //define field as attribute
	private String studentName;
	
	@XStreamImplicit        //define list as an implicit collection
	private List<Note> notes = new ArrayList<Note>();
	
	@XStreamOmitField		//omit a field to not to be a part of XML
	private int type;
}

讓我們使用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.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
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();
      xstream.processAnnotations(Student.class);		

      //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."));
	  student.setType(1);
      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;
      }
   }
}

@XStreamAlias("student")
class Student {

   @XStreamAlias("name")
   @XStreamAsAttribute
   private String studentName;

   @XStreamImplicit
   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;
   }
   
   @XStreamOmitField		
   private int type;

   public int getType(){
      return type;
   }

   public void setType(int type){
      this.type = type;
   }
}

@XStreamAlias("note")
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"?>
<student name="Mahesh">
  <note>
    <title>first</title>
    <description>My first assignment.</description>
  </note>
  <note>
    <title>second</title>
    <description>My Second assignment.</description>
  </note>
</student>

為了告訴XStream框架來處理注釋,需要XML序列化之前添加下麵的命令。

xstream.processAnnotations(Student.class);		
或者
xstream.autodetectAnnotations(true);