📜  XStream-别名

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


别名是一种自定义生成的XML或使用XStream使用特定格式的XML的技术。假设以下XML格式用于对Student对象进行序列化/反序列化。


   
      first
      My first assignment.
   
   
   
      second
      My second assignment.
   

基于上述XML格式,让我们创建模型类。

class Student {
   private String studentName;
   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;
   }
}

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测试上述对象的序列化。

在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.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 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;
   }
}

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

验证输出如下-



   Mahesh
   
      
         first
         My first assignment.
      
      
      
         second
         My Second assignment.
     
   

在以上结果中,学生对象名称是完全合格的。要将其替换为学生标签,请遵循下一部分。