📜  XStream-批注

📅  最后修改于: 2020-11-10 05:18:19             🧑  作者: Mango


XStream支持注释,类似于自动配置而不是编码。在上一章中,我们已经在代码中看到了以下配置。

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

以下代码段说明了使用注释以更简单的方式完成相同工作的方法。

@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 notes = new ArrayList();
   
   @XStreamOmitField       //omit a field to not to be a part of XML
   private int type;
}

让我们使用XStream测试上面的注释。

C:\> XStream_WORKSPACE \ com \ tutorialspoint \ xstream中创建一个名为XStreamTester的Java类文件。

档案:XStreamTester.java

package com.tutorialspoint.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 notes = new ArrayList();

   public Student(String name) {
      this.studentName = name;
   }

   public void addNote(Note note) {
      notes.add(note);
   }

   public String getName() {
      return studentName;
   }

   public List 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\tutorialspoint\xstream>javac XStreamTester.java

现在运行XStreamTester以查看结果-

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

验证输出如下-



   
      first
      My first assignment.
   

   
      second
      My Second assignment.
   

为了指示XStream框架处理注释,需要在序列化xml之前添加以下命令。

xstream.processAnnotations(Student.class);        

要么

xstream.autodetectAnnotations(true);