当前位置: 首页 > 文档资料 > XStream 中文教程 >

场别名(Field Aliasing)

优质
小牛编辑
130浏览
2023-12-01

字段别名用于在XML中创建字段的别名。 让我们再次修改我们的示例并将以下代码添加到其中。

xstream.aliasField("studentName", Student.class, "name");

让我们使用XStream测试上面对象的序列化。

C:\》XStream_WORKSPACE\com\xnip\xstream创建名为XStreamTester的java类文件。

File: XStreamTester.java

package cn.xnip.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());
      xstream.alias("student", Student.class);
      xstream.alias("note", Note.class);
      xstream.aliasField("name", Student.class, "studentName");
      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\xnip\xstream>javac XStreamTester.java

现在运行XStreamTester来查看结果 -

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

验证输出如下 -

<?xml version = "1.0" encoding = "UTF-8"?>
<student>
   <name>Mahesh</name>
   <notes>
      <note>
         <title>first</title>
         <description>My first assignment.</description>
      </note>
      <note>
         <title>second</title>
         <description>My Second assignment.</description>
      </note>
   </notes>
</student>

在上面的结果中,我们可以看到notes标签被添加为注释列表。 要替换它,请按照下一节进行操作。