zoukankan      html  css  js  c++  java
  • conversion between xml and object using xstream

      offical site said, but it does not make it clearly. especial parse xml with attributes. here is my test according to docs from offical websites.

    public class XstreamTest {
        
        public static void main(String[] args) throws Exception {
            //convertXml2();
            convertObject2();
    
        }
        
        public static void convertXml1(){
            RendezvousMessage1 message = new RendezvousMessage1(1, false, "part1","part2","part3");
            XStream xStream = new XStream(new DomDriver());
            xStream.processAnnotations(RendezvousMessage1.class);
            System.out.println("message1 "+xStream.toXML(message));
        }
        public static String convertXml2(){
            RendezvousMessage2 message = new RendezvousMessage2(1, false, "part1","part2","part3");
            XStream xStream = new XStream(new DomDriver());
            xStream.processAnnotations(RendezvousMessage2.class);
            System.out.println("message2 "+xStream.toXML(message));
            String xmlString = xStream.toXML(message);
            return xmlString;
        }
        
        public static void convertObject2(){
            
            XStream xStream = new XStream(new DomDriver());
            xStream.processAnnotations(RendezvousMessage2.class);
            System.out.println("convertObject2 "+xStream.fromXML(convertXml2()));
        }
        
    
    }
    
    @XStreamAlias("message")
    class RendezvousMessage1 {
    
        @XStreamAlias("type")
           @XStreamAsAttribute
        private int messageType;
    
        @XStreamImplicit(itemFieldName="part")
        private List<String> content;
        
           @XStreamAsAttribute
        @XStreamConverter(value=BooleanConverter.class, booleans={false}, strings={"yes", "no"})
        private boolean important;
    
    
        public RendezvousMessage1(int messageType, boolean important, String... content) {
            this.messageType = messageType;
            this.important = important;
            this.content = Arrays.asList(content);
        }
    }
    @XStreamAlias("message")
    class RendezvousMessage2 {
    
           @XStreamAsAttribute
        private int type;
    
        @XStreamImplicit(itemFieldName="part")
        private List<String> content;
        
           @XStreamAsAttribute
        @XStreamConverter(value=BooleanConverter.class, booleans={false}, strings={"yes", "no"})
        private boolean important;
    
    
        public RendezvousMessage2(int messageType, boolean important, String... content) {
            this.type = messageType;
            this.important = important;
            this.content = Arrays.asList(content);
        }
        @Override
        public String toString() {
            return "type "+type+" content "+content;
        }
    }

      through my test , it work well.

  • 相关阅读:
    unity ab包打包和加载的简单学习示例
    项目整理回顾1,关于单例使用
    关于lua闭包导致引用无法释放内存泄露
    unity lua require dofile loadfile 区别
    unity editor模式下读取文件夹资源
    unity texture 占用内存大小对比
    关于unity贴图压缩
    内聚,耦合,解耦和,依赖倒置
    lua type 获取 类型
    Unity+NGUI多分辨率适配方案
  • 原文地址:https://www.cnblogs.com/slider/p/2987876.html
Copyright © 2011-2022 走看看