xStream完美转换XML、JSON
2016-05-11 17:26
791 查看
/**
* 将文本消息对象转为xml 讲java bean 转换成xml
* @param textMessage
* @return
*/
public static String textMessageToXml(TextMessage textMessage){
XStream xstream = new XStream();
xstream.alias("xml", textMessage.getClass());
return xstream.toXML(textMessage);
}
1.xstream的alias使用方法:
1.1 作用:将序列化中的类全量名称,用别名替换。
1.2 使用方法:xstream.alias("blog", Blog.class);
1.3 示例:
要序列化的类:
package test.xstream.test;
public class Author {
private String name;
public Author(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
不使用别名alias时序列化出来的xml:
<test.xstream.test.Author>
<name>name</name>
</test.xstream.test.Author>
使用别名alias时序列化出来的xml:
<Author>
<name>name</name>
</Author>
2.xstream的aliasField
2.1 作用:使用别名替代属性名
2.2 使用方法:xstream.aliasField("author", Author.class, "name");
2.3 示例:
不使用别名aliasField时序列化出来的xml:
<Author>
<name>name</name>
</Author>
使用别名aliasField时序列化出来的xml:
<Author>
<author>name</author>
</Author>
3. xstream的useAttributeFor
3.1 作用:将某一个类的属性,作为xml头信息的属性,而不是子节点
3.2 使用方法:xstream.useAttributeFor(Author.class, "name");
3.3 示例: 不使用别名useAttributeFor时序列化出来的xml:
<Author> <author>name</author> </Author>
使用别名useAttributeFor时序列化出来的xml:
<Author name="name"/>
ps: 使用方法
public static void main(String[] args) {
XStream xstream = new XStream();
xstream.alias("Author", Author.class);
// xstream.aliasField("author", Author.class, "name");
xstream.useAttributeFor(Author.class, "name");
Author author =new Author("name");
String xmlString =xstream.toXML(author);
System.out.println(xmlString);
}
几个相关网址:http://xstream.codehaus.org/alias-tutorial.html
http://blog.csdn.net/faye0412/article/details/6602144
* 将文本消息对象转为xml 讲java bean 转换成xml
* @param textMessage
* @return
*/
public static String textMessageToXml(TextMessage textMessage){
XStream xstream = new XStream();
xstream.alias("xml", textMessage.getClass());
return xstream.toXML(textMessage);
}
1.xstream的alias使用方法:
1.1 作用:将序列化中的类全量名称,用别名替换。
1.2 使用方法:xstream.alias("blog", Blog.class);
1.3 示例:
要序列化的类:
package test.xstream.test;
public class Author {
private String name;
public Author(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
不使用别名alias时序列化出来的xml:
<test.xstream.test.Author>
<name>name</name>
</test.xstream.test.Author>
使用别名alias时序列化出来的xml:
<Author>
<name>name</name>
</Author>
2.xstream的aliasField
2.1 作用:使用别名替代属性名
2.2 使用方法:xstream.aliasField("author", Author.class, "name");
2.3 示例:
不使用别名aliasField时序列化出来的xml:
<Author>
<name>name</name>
</Author>
使用别名aliasField时序列化出来的xml:
<Author>
<author>name</author>
</Author>
3. xstream的useAttributeFor
3.1 作用:将某一个类的属性,作为xml头信息的属性,而不是子节点
3.2 使用方法:xstream.useAttributeFor(Author.class, "name");
3.3 示例: 不使用别名useAttributeFor时序列化出来的xml:
<Author> <author>name</author> </Author>
使用别名useAttributeFor时序列化出来的xml:
<Author name="name"/>
ps: 使用方法
public static void main(String[] args) {
XStream xstream = new XStream();
xstream.alias("Author", Author.class);
// xstream.aliasField("author", Author.class, "name");
xstream.useAttributeFor(Author.class, "name");
Author author =new Author("name");
String xmlString =xstream.toXML(author);
System.out.println(xmlString);
}
几个相关网址:http://xstream.codehaus.org/alias-tutorial.html
http://blog.csdn.net/faye0412/article/details/6602144
相关文章推荐
- 获取屏幕宽高
- maven项目无法使用jstl解决办法
- CI笔记6 json 传值
- JsonHelper 操作帮助类
- JavaScript 知识点之escape()与decodeURI()
- javascript实例学习之六—百叶窗效果
- 使用jstl+el表达式遇到的几个问题
- JavaScript 页面间传值
- 利用jsp+uploadify插件实现附件上传到ftp服务器的功能
- js模拟重载
- 如何将杂乱无章的js文件格式化
- 关于JavaScript的事件触发
- .net动态解析xml和json
- js限制上传文件类型和大小
- js技巧
- 前端开发中最常用的JS代码片段
- javascript小技巧
- JavaScript(ECMAScript) with 语句
- 在jsp页面中对浏览器类型判断~
- JSP基本语法