1.下载附件中的xstream-1.1.3.jar包
2.建立一个文件Integral.java代码如下:
public class Integral {
private int caseId;
private Date surveyCompleteTime;
public int getCaseId() {
return caseId;
}
public void setCaseId(int caseId) {
this.caseId = caseId;
}
public Date getSurveyCompleteTime() {
return surveyCompleteTime;
}
public void setSurveyCompleteTime(Date surveyCompleteTime) {
this.surveyCompleteTime = surveyCompleteTime;
}
public static void main(String[] args) {
XStream xstream = new XStream(new DomDriver());
xstream.alias("Integral", Integral.class);
Integral i = new Integral();
i.setSurveyCompleteTime(Calendar.getInstance().getTime());
i.setCaseId(120);
String xml = xstream.toXML(i);
System.out.println(xml);
Integral i2 = (Integral) xstream.fromXML(xml);
System.out.println(i2.getCaseId() + "|" + i2.getSurveyCompleteTime());
}
}
2.建立一个文件Integral.java代码如下:
public class Integral {
private int caseId;
private Date surveyCompleteTime;
public int getCaseId() {
return caseId;
}
public void setCaseId(int caseId) {
this.caseId = caseId;
}
public Date getSurveyCompleteTime() {
return surveyCompleteTime;
}
public void setSurveyCompleteTime(Date surveyCompleteTime) {
this.surveyCompleteTime = surveyCompleteTime;
}
public static void main(String[] args) {
XStream xstream = new XStream(new DomDriver());
xstream.alias("Integral", Integral.class);
Integral i = new Integral();
i.setSurveyCompleteTime(Calendar.getInstance().getTime());
i.setCaseId(120);
String xml = xstream.toXML(i);
System.out.println(xml);
Integral i2 = (Integral) xstream.fromXML(xml);
System.out.println(i2.getCaseId() + "|" + i2.getSurveyCompleteTime());
}
}
本文介绍如何使用XStream库将Java对象序列化为XML格式,并演示了一个具体示例,包括创建对象、序列化过程及反序列化。
1376

被折叠的 条评论
为什么被折叠?



