XStream-앨리어싱
앨리어싱은 생성 된 XML을 사용자 지정하거나 XStream을 사용하여 특정 형식의 XML을 사용하는 기술입니다. 다음 XML 형식을 사용하여 Student 개체를 직렬화 / 역 직렬화한다고 가정 해 보겠습니다.
<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 \ tutorialspoint \ xstream에 XStreamTester라는 Java 클래스 파일을 만듭니다.
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
다음을 사용하여 클래스 컴파일 javac 다음과 같이 컴파일러-
C:\XStream_WORKSPACE\com\tutorialspoint\xstream>javac XStreamTester.java
이제 XStreamTester를 실행하여 결과를 확인하십시오.
C:\XStream_WORKSPACE\com\tutorialspoint\xstream>java XStreamTester
다음과 같이 출력을 확인하십시오-
<?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>
위의 결과에서 Student 개체 이름은 정규화되었습니다. 학생 태그로 바꾸려면 다음 섹션을 따르십시오.
클래스 별칭
필드 앨리어싱
암시 적 컬렉션 별칭
속성 별칭
패키지 별칭