xstream 别名的用法

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
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值