一、方法:
1、xs.alias :别名
2、xs.fromXML : XML转换为java对象
3、xs.toXML java对象转换为XML
二、代码:
E:/t.txt文件中内容:
<xml>
<ToUserName>1</ToUserName>
<FromUserName>1</FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType>1</MsgType>
<Content>1</Content>
</xml>
java测试代码:
public static void main(String[] args){
XStream xs = new XStream();
try {
InputStream in = new FileInputStream("E:/t.txt");
TextRespMessage t=new TextRespMessage();
t.setContent("1");
t.setCreateTime(System.currentTimeMillis());
t.setMsgType("text");
// xs.alias("xml", TextRespMessage.class);
System.out.println(xs.toXML(t));
} catch (IOException e) {
e.printStackTrace();
}
}
1、toXML方法未加 alias方法 结果:
2、toXML 方法加入alias 方法结果,可见com.gusy.course.message.resp.TextRespMessage变成了xml:
三、知识拓展:InputStreamReader练习
public static void main(String[] args){
try {
InputStream in = new FileInputStream("E:/t.txt");
InputStreamReader r = new InputStreamReader(in, "utf-8");//IO流知识中唯一可以转换字符编码的方法
BufferedReader br = new BufferedReader(r);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}