zoukankan      html  css  js  c++  java
  • XStream 遇到的问题

      1.xstream xml转bean对象,双划线问题

       解决:主要是用到了new XmlFriendlyNameCoder("_-", "_"),在创建XStream对象时可XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_")));

      2.获取节点的属性值以及自身值

        方法1:@XStreamConverter(value=ToAttributedValueConverter.class, strings={"value"})

               对应的value是bean对象的属性名,具体示例:

    @XStreamAlias("RETURNCODE")
    @XStreamConverter(value=ToAttributedValueConverter.class, strings={"value"})
    public class ReturnCodeVO {
    
        @XStreamAsAttribute
        @XStreamAlias("CODE")
        private String code;
    
        @XStreamAsAttribute
        @XStreamAlias("MSG")
        private String msg;
    
        @XStreamAsAttribute
        @XStreamAlias("CMSG")
        private String cmsg;
    
        private String value;
    
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public String getCmsg() {
            return cmsg;
        }
    
        public void setCmsg(String cmsg) {
            this.cmsg = cmsg;
        }
    
        public String getValue() {
            return value;
        }
    
        public void setValue(String value) {
            this.value = value;
        }
    }

        方法2:使用转换器Converter,具体示例如下:

      

    public class CityConverter implements Converter {
    
        public void marshal(Object value, HierarchicalStreamWriter writer, 
                                                                   MarshallingContext context) {
            City city = (City) value;
            writer.addAttribute("id", city.getId());
            writer.addAttribute("type", city.getType().toString());
            writer.setValue(city.getName());
        }
    
        public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
            City city = new City();
            city.setName(reader.getValue());
            city.setId(reader.getAttribute("id"));
            city.setType(reader.getAttribute("type"));
            return city;
        }
    
        public boolean canConvert(Class clazz) {
            return clazz.equals(City.class);
        }
    
    }

      调用XStream时进行设置:

      XStream xStream = new XStream();

      xStream.registerConverter(new CityConverter());

       xStream.alias("loc", City.class);

  • 相关阅读:
    HTML当中特殊字符的表示
    溢出文本用“...”代替
    【转】图标字体化浅谈
    字体在网页中画ICON图标
    图片轮播
    js获取网页屏幕可视区域高度
    MVC入口程序 | 简单调用及实例化
    初学者对于MVC架构模式学习与理解
    PHP初学习笔记(2015/4/8)
    linux常用20命令 --转载
  • 原文地址:https://www.cnblogs.com/zgz21/p/8626861.html
Copyright © 2011-2022 走看看