美文网首页
(5)xstream的注解方式的使用

(5)xstream的注解方式的使用

作者: Mrsunup | 来源:发表于2018-10-24 21:23 被阅读0次

    在工作中,发下xml的解析其实用得非常的少,实际在工作中一般用xstream的第三方的jar包比较多,这个jar可以很方便的实现xml到javabeans的转换,使用起来非常的简单方便

    参考学习文档:http://x-stream.github.io/tutorial.html
    建议可以先参考文档中Tutorials教程部分,xstream可以使用java的编程方式来给实体列别名,也可以使用注解的方式来实现别名

    1.xstream的注解方式的使用

    要求: 使用xstream的api,完成xml到javabean的转换以及javabean到json的转换

    • 依赖的jar包

    jettison为可以实现json到javabean的转换方式

        <dependency>
          <groupId>com.thoughtworks.xstream</groupId>
          <artifactId>xstream</artifactId>
          <version>1.4.9</version>
        </dependency>
      <dependency>
          <groupId>org.codehaus.jettison</groupId>
          <artifactId>jettison</artifactId>
          <version>1.3.7</version>
        </dependency>
    
    • beans.xml内容如下:
    <beans>
        <bean id="bean1" class ="com.java.jaxp.dom">
              <property name="username">sunkang</property>
        </bean>
        <bean id="bean2" class ="com.java.jaxp.dom">
            <property name="name">sunkang</property>
        </bean>
    </beans>
    
    • javabeans的实体
    /**
     * @Project: jdk
     * @description:  javabeans的实体
     * @author: sunkang
     * @create: 2018-10-19 10:42
     * @ModificationHistory who      when       What
     **/
    
    @XStreamAlias("beans")
    public class BeansEntity {
    
        @XStreamImplicit
        public List<Bean> beans;
    
        public BeansEntity(List<Bean> beans) {
            this.beans = beans;
        }
    
        @Override
        public String toString() {
            return "Beans{" +
                    "bean=" + beans +
                    '}';
        }
    }
    
    
    @XStreamAlias("bean")
    class Bean{
        @XStreamImplicit
        public List<Property> properties;
    
        @XStreamAsAttribute
        public  String id;
    
        @XStreamAsAttribute
        @XStreamAlias("class")
        public  String clazz;
    
        public Bean(List<Property> properties, String id, String clazz) {
            this.properties = properties;
            this.id = id;
            this.clazz = clazz;
        }
    
        @Override
        public String toString() {
            return "Bean{" +
                    "properties=" + properties +
                    ", id='" + id + '\'' +
                    ", clazz='" + clazz + '\'' +
                    '}';
        }
    }
    
    @XStreamAlias("property")
    //下面ToAttributedValueConverter的作用是把其他全部变成属性值,指定的为值
    @XStreamConverter(value=ToAttributedValueConverter.class, strings={"value"})
    class Property{
        @XStreamAsAttribute
        public String name ;
    
        public  String value;
    
        public Property(String name, String value) {
            this.name = name;
            this.value = value;
        }
    
        @Override
        public String toString() {
            return "Property{" +
                    "name='" + name + '\'' +
                    ", value='" + value + '\'' +
                    '}';
        }
    }
    
    
    • xstream解析
    /**
     * @Project: jdk
     * @description:  xstream 实现了xml到javabeans的转换,javabean到json的转换
     * @author: sunkang
     * @create: 2018-10-19 10:38
     * @ModificationHistory who      when       What
     **/
    public class XstreamDemo {
    
        public static void main(String[] args) {
    
            XStream xStream = new XStream(new DomDriver());
            xStream.processAnnotations(BeansEntity.class);
    
            Property property1 =new Property("sun","kang");
            Property property2 =new Property("kang","sun");
            Property property3 =new Property("sun2","kang2");
            List<Property> properties1 = new ArrayList<Property>();
            properties1.add(property1);
            properties1.add(property3);
            List<Property> properties2 = new ArrayList<Property>();
            properties2.add(property2);
    
            Bean bean1 = new Bean(properties1,"id1","class1");
            Bean bean2 = new Bean(properties2,"id2","class2");
            List<Bean> beans = new ArrayList<Bean>();
            beans.add(bean1);
            beans.add(bean2);
            System.out.println("-------------把java实体转化成xml对象--------------------");
            //把java实体转化成xml对象
            BeansEntity entity = new BeansEntity(beans);
            String xmlMsg = xStream.toXML(entity);
            System.out.println(xmlMsg);
    
    
            System.out.println("-------------用xml转化成java实体-------");
            //用xml转化成java实体
             BeansEntity  beansEntity = (BeansEntity) xStream.fromXML(xmlMsg);
             System.out.println(beansEntity);
    
            System.out.println("-------------实体转化成json--------------------");
            //转化成json ,需要增加  jettison的jar包
            XStream xstreamTojson = new XStream(new JettisonMappedXmlDriver());
            xstreamTojson.setMode(XStream.NO_REFERENCES);
            xstreamTojson.processAnnotations(BeansEntity.class);
            String json = xstreamTojson.toXML(entity);
            System.out.println(json);
    
            System.out.println("-------------json 转化成java--------------------");
            //json 转化成java
            BeansEntity beansEntity1 = (BeansEntity) xstreamTojson.fromXML(json);
            System.out.println(beansEntity1);
        }
    }
    
    • 输出结果
    -------------把java实体转化成xml对象--------------------
    <beans>
      <bean id="id1" class="class1">
        <property name="sun">kang</property>
        <property name="sun2">kang2</property>
      </bean>
      <bean id="id2" class="class2">
        <property name="kang">sun</property>
      </bean>
    </beans>
    -------------用xml转化成java实体-------
    Beans{bean=[Bean{properties=[Property{name='sun', value='kang'}, Property{name='sun2', value='kang2'}], id='id1', clazz='class1'}, Bean{properties=[Property{name='kang', value='sun'}], id='id2', clazz='class2'}]}
    -------------实体转化成json--------------------
    {"beans":{"bean":[{"@id":"id1","@class":"class1","property":[{"@name":"sun","$":"kang"},{"@name":"sun2","$":"kang2"}]},{"@id":"id2","@class":"class2","property":{"@name":"kang","$":"sun"}}]}}
    -------------json 转化成java--------------------
    Beans{bean=[Bean{properties=[Property{name='sun', value='kang'}, Property{name='sun2', value='kang2'}], id='id1', clazz='class1'}, Bean{properties=[Property{name='kang', value='sun'}], id='id2', clazz='class2'}]}
    

    相关文章

      网友评论

          本文标题:(5)xstream的注解方式的使用

          本文链接:https://www.haomeiwen.com/subject/hsqftqtx.html