attribute java c_属性别名(Attribute Aliasing)
屬性別名(Attribute Aliasing)
屬性別名用于將成員變量序列化為XML屬性。 讓我們再次修改我們的示例并將以下代碼添加到其中。xstream.useAttributeFor(Student.class, "studentName");
xstream.aliasField("name", Student.class, "studentName");
讓我們使用XStream測試上面對象的序列化。
在C:\》XStream_WORKSPACE\com\iowiki\xstream創(chuàng)建名為XStreamTester的java類文件。
File: XStreamTester.javapackage com.iowiki.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.useAttributeFor(Student.class, "studentName");
xstream.aliasField("name", Student.class, "studentName");
xstream.addImplicitCollection(Student.class, "notes");
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;
}
}
驗證結(jié)果
使用javac編譯器編譯類如下 -C:\XStream_WORKSPACE\com\iowiki\xstream>javac XStreamTester.java
現(xiàn)在運行XStreamTester來查看結(jié)果 -C:\XStream_WORKSPACE\com\iowiki\xstream>java XStreamTester
驗證輸出如下 -<?xml version = "1.0" encoding = "UTF-8"?>
firstMy first assignment.
secondMy Second assignment.
總結(jié)
以上是生活随笔為你收集整理的attribute java c_属性别名(Attribute Aliasing)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 将四个整数进行从小到大的顺序排列 jav
- 下一篇: python grep 列表_关于pyt