XStream - Aliasing
Aliasing é uma técnica para personalizar o XML gerado ou usar um determinado XML formatado usando XStream. Vamos supor que o seguinte formato XML seja usado para serializar / desserializar o objeto Aluno.
<student name = "Suresh">
<note>
<title>first</title>
<description>My first assignment.</description>
</note>
<note>
<title>second</title>
<description>My second assignment.</description>
</note>
</student>
Com base no formato XML acima, vamos criar classes de modelo.
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;
}
}
Vamos testar a serialização dos objetos acima usando o XStream.
Crie um arquivo de classe java denominado XStreamTester em C: \> XStream_WORKSPACE \ com \ tutorialspoint \ xstream.
File: 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<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;
}
}
Verify the Result
Compile as classes usando javac compilador da seguinte forma -
C:\XStream_WORKSPACE\com\tutorialspoint\xstream>javac XStreamTester.java
Agora execute o XStreamTester para ver o resultado -
C:\XStream_WORKSPACE\com\tutorialspoint\xstream>java XStreamTester
Verifique a saída da seguinte forma -
<?xml version = "1.0" encoding = "UTF-8"?>
<com.tutorialspoint.xstream.Student>
<studentName>Mahesh</studentName>
<notes>
<com.tutorialspoint.xstream.Note>
<title>first</title>
<description>My first assignment.</description>
</com.tutorialspoint.xstream.Note>
<com.tutorialspoint.xstream.Note>
<title>second</title>
<description>My Second assignment.</description>
</com.tutorialspoint.xstream.Note>
</notes>
</com.tutorialspoint.xstream.Student>
No resultado acima, o nome do objeto Aluno é totalmente qualificado. Para substituí-la como etiqueta de aluno, siga a próxima seção.
Aliasing de classe
Aliasing de campo
Aliasing de coleções implícitas
Atributo Aliasing
Aliasing de pacote