详解XStream别名
admin
2023-08-02 00:20:08
0

在上一节中,我们已经看到了XStream是多么的简单易用,本文将继续以实例的方式讲解XStream别名。毕竟,你的JAVA对象不可能总是与XML结构毫发无差的,谁也不确定某个开发者手误打错了字母,或者是报文的名称发生了变化。


假设输出如下的XML结构,我们应该怎么做呢?


    
      第一篇
      欢迎来到木瓜博客!
    
    
      第二篇
      全国启动防雾霾红色预警。
    
  


1.  根据XML结构构建JAVA对象

package com.favccxx.favsoft.pojo;
import java.util.ArrayList;
import java.util.List;
public class Blog {
    private Author writer;
    private List entries = new ArrayList();
    public Blog(Author writer) {
        this.writer = writer;
    }
    public void add(Entry entry) {
        entries.add(entry);
    }
    public void addEntry(Entry entry){
        entries.add(entry);
    }
    public List getContent() {
        return entries;
    }
    public Author getWriter() {
        return writer;
    }
    public void setWriter(Author writer) {
        this.writer = writer;
    }
}


package com.favccxx.favsoft.pojo;
public class Entry {
    private String title;
    private String description;
    public Entry(String title, String description) {
        this.title = title;
        this.description = description;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}


2.开始代码测试

package com.favccxx.favsoft.main;
import com.favccxx.favsoft.pojo.Author;
import com.favccxx.favsoft.pojo.Blog;
import com.favccxx.favsoft.pojo.Entry;
import com.thoughtworks.xstream.XStream;
public class MainBlog {
    public static void main(String args[]) {
        Blog myBlog = new Blog(new Author("一个木瓜"));
        myBlog.add(new Entry("第一篇", "欢迎来到木瓜博客!"));
        myBlog.add(new Entry("第二篇", "全国启动防雾霾红色预警。"));
        XStream xstream = new XStream();
        System.out.println(xstream.toXML(myBlog));
    }
}


3.发现输出内容结构并不是想象的那样,怎么办?


  
    一个木瓜
  
  
    
      第一篇
      欢迎来到木瓜博客!
    
    
      第二篇
      全国启动防雾霾红色预警。
    
  


4.使用类别名,将包含路径的类对象进行替换。

xstream.alias("blog", Blog.class);
xstream.alias("entry", Entry.class);


发现输出结构与想要的结构近了一点,但还是不满足要求。


  
    一个木瓜
  
  
    
      第一篇
      欢迎来到木瓜博客!
    
    
      第二篇
      全国启动防雾霾红色预警。
    
  


5. 使用字段别名,将写手writer改成作者author,发现输出结构又近了一层。

xstream.aliasField("author", Blog.class, "writer");



  
    一个木瓜
  
  
    
      第一篇
      欢迎来到木瓜博客!
    
    
      第二篇
      全国启动防雾霾红色预警。
    
  


6. 使用隐式集合,将不需要展示的集合的根节点进行隐藏。需要注意的是数组和MAP结合不能声明成隐式集合。

xstream.addImplicitCollection(Blog.class, "entries");



  
    一个木瓜
  
  
    第一篇
    欢迎来到木瓜博客!
  
  
    第二篇
    全国启动防雾霾红色预警。
  


7. 使用属性别名,将xml中的成员对象以属性标签形式展示。但是属性标签并不会直接写到xml标签上去,需要实现SingleValueConverter转换器才行。

xstream.useAttributeFor(Blog.class, "writer");
xstream.aliasField("author", Blog.class, "writer");


package com.favccxx.favsoft.util;
import com.favccxx.favsoft.pojo.Author;
import com.thoughtworks.xstream.converters.SingleValueConverter;
public class AuthorConverter implements SingleValueConverter {
    @Override
    public boolean canConvert(Class type) {
        return type.equals(Author.class);
    }
    @Override
    public String toString(Object obj) {
        return ((Author) obj).getName();
    }
    @Override
    public Object fromString(String str) {
        return new Author(str);
    }
}


8.终于改完了,最终的完整代码是这样的

package com.favccxx.favsoft.main;
import com.favccxx.favsoft.pojo.Author;
import com.favccxx.favsoft.pojo.Blog;
import com.favccxx.favsoft.pojo.Entry;
import com.favccxx.favsoft.util.AuthorConverter;
import com.thoughtworks.xstream.XStream;
public class MainBlog {
    public static void main(String args[]) {
        Blog myBlog = new Blog(new Author("一个木瓜"));
        myBlog.add(new Entry("第一篇", "欢迎来到木瓜博客!"));
        myBlog.add(new Entry("第二篇", "全国启动防雾霾红色预警。"));
        XStream xstream = new XStream();
        xstream.alias("blog", Blog.class);
        xstream.alias("entry", Entry.class);
        xstream.useAttributeFor(Blog.class, "writer");
        xstream.aliasField("author", Blog.class, "writer");
        xstream.registerConverter(new AuthorConverter());
        xstream.addImplicitCollection(Blog.class, "entries");
        System.out.println(xstream.toXML(myBlog));
    }
}


9.输出完美的XML结构


  
    第一篇
    欢迎来到木瓜博客!
  
  
    第二篇
    全国启动防雾霾红色预警。
  


10.有时候需要使用包别名,将包名进行替换。需要替换的包名必须从首字母开始替换,不能从中间开始查找替换。

package com.favccxx.favsoft.main;
import com.favccxx.favsoft.pojo.Author;
import com.favccxx.favsoft.pojo.Blog;
import com.favccxx.favsoft.pojo.Entry;
import com.thoughtworks.xstream.XStream;
public class MainBlog {
    public static void main(String args[]) {
        Blog myBlog = new Blog(new Author("一个木瓜"));
        myBlog.add(new Entry("第一篇", "欢迎来到木瓜博客!"));
        myBlog.add(new Entry("第二篇", "全国启动防雾霾红色预警。"));
        XStream xstream = new XStream();
        xstream.aliasPackage("my.company", "com.favccxx");
        System.out.println(xstream.toXML(myBlog));
    }
}


11.输出格式如下


  
    一个木瓜
  
  
    
      第一篇
      欢迎来到木瓜博客!
    
    
      第二篇
      全国启动防雾霾红色预警。
    
  


小结:

  • 可以使用类别名改变标签的名称

  • 可以使用字段别名改变标签的名称

  • 可以使用包别名改变标签的名称

  • 通过实现SingleValueConverter接口,可以将成员字段显示到对象属性标签上


相关内容

热门资讯

广西村民被蛇咬伤撑了两天后离世... 封面新闻记者 杨峰 广西横州报道受2026年第10号台风“美莎克”影响,7月6日广西横州市六蓝水库发...
圆通、中通、韵达等发布告客户书 7月9日,圆通速递、中通快递等多家物流企业相继发布告客户书,提醒受今年第 9 号超强台风 "巴威" ...
市政协领导分赴一线督查指导防汛... 今年第9号台风“巴威”(超强台风级)逐步逼近,温州市气象台于7月9日19时5分发布温州市区台风蓝色预...
原创 七... 2025年度国家科学技术奖揭晓,韦东奕凭借《流动转捩机理的数学研究》,斩获国家自然科学奖二等奖。 虽...
上虞:抢抓防台“窗口期” 提前... 【本站7月9日讯】为有效防范今年第9号台风“巴威”,上虞区坚持关口前移、提前部署,坚持“一个目标、四...
男子隐私部位被割断,医生接回!... 6月某天的中午,25岁的小张经历了一场突如其来的意外,阴茎遭受利刃伤害,“命根子”险些没了。
消息人士:穆杰塔巴不太可能出席... 据CNN报道,一位地区消息人士透露,伊朗新任最高领袖穆杰塔巴不太可能按照伊斯兰革命卫队的建议,出席其...
内塔尼亚胡与特朗普通话,商定保... 当地时间9日晚间,以色列总理办公室发表声明称,以总理内塔尼亚胡当晚与美国总统特朗普通电话,双方商定将...
华电科工获得实用新型专利授权:... 证券之星消息,根据天眼查APP数据显示华电科工(601226)新获得一项实用新型专利授权,专利名为“...
中集集团获得实用新型专利授权:... 证券之星消息,根据天眼查APP数据显示中集集团(000039)新获得一项实用新型专利授权,专利名为“...