• 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);

  • 相关阅读:
    向net core 3.0进击——多平台项目发布与部署
    Linux配置部署_新手向(五)——Docker的安装与使用
    Ubuntu 18.04使用OpenSSL自签证书(证书支持多IP及多域名,谷歌浏览器无警告)
    学习makefile的一个工程示例
    Centos7下设置ceph 12.2.1 (luminous)dashboard UI监控功能
    一个小例子学习makefile
    Centos7下部署ceph 12.2.1 (luminous)集群及RBD使用
    VMware Centos7 桥接 DHCP无法获得IP
    VMware Centos7 NAT 无法上网的解决方法
    jerasure 2.0译文
  • 原文地址:https://www.cnblogs.com/zgz21/p/8626861.html
Copyright © 2020-2023  润新知