Xstream-aliasing

提供:Dev Guides
移動先:案内検索

XStream-エイリアシング

エイリアシングは、生成されたXMLをカスタマイズするか、XStreamを使用して特定のフォーマットされたXMLを使用する技術です。 Studentオブジェクトのシリアル化/逆シリアル化に次のXML形式を使用するとします。

<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を使用して上記のオブジェクトのシリアル化をテストしましょう。

C:\> XStream_WORKSPACE \ com \ finddevguides \ xstreamにXStreamTesterという名前のJavaクラスファイルを作成します。

*_ファイル:XStreamTester.java_*
package com.finddevguides.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\finddevguides\xstream>javac XStreamTester.java

今、結果を見るためにXStreamTesterを実行します-

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

次のように出力を確認します-

<?xml version = "1.0" encoding = "UTF-8"?>
<com.finddevguides.xstream.Student>
   <studentName>Mahesh</studentName>
   <notes>
      <com.finddevguides.xstream.Note>
         <title>first</title>
         <description>My first assignment.</description>
      </com.finddevguides.xstream.Note>

      <com.finddevguides.xstream.Note>
         <title>second</title>
         <description>My Second assignment.</description>
     </com.finddevguides.xstream.Note>
   </notes>
</com.finddevguides.xstream.Student>

上記の結果では、Studentオブジェクト名は完全修飾されています。 学生タグとして置き換えるには、次のセクションに従ってください。

  • リンク:/xstream/xstream_class_aliasing [クラスのエイリアス]
  • リンク:/xstream/xstream_field_aliasing [フィールドのエイリアス]
  • リンク:/xstream/xstream_implicit_aliasing [Implicit Collections Aliasing]
  • リンク:/xstream/xstream_attribute_aliasing [属性のエイリアス]
  • リンク:/xstream/xstream_package_aliasing [パッケージのエイリアス]