码迷,mamicode.com
首页 > 编程语言 > 详细

java与xml转换 -- XStreamAlias

时间:2018-06-28 19:21:56      阅读:444      评论:0      收藏:0      [点我收藏+]

标签:ati   注解   import   tox   持久   mes   version   pom   tag   


@XStreamAlias

1.特点


  简化的API;
  无映射文件;
  高性能,低内存占用;
  整洁的XML;
  不需要修改对象;支持内部私有字段,不需要setter/getter方法
  提供序列化接口;
  自定义转换类型策略;

2.使用场景


  Transport转换
  Persistence 持久化对象
  Configuration 配置
  Unit Tests 单元测试
  Interface服务之间XML消息的交互

3.model


实体类

import com.thoughtworks.xstream.annotations.XStreamAlias;

/**
 * @author ceshi
 * @Title: Student
 * @ProjectName ceshi
 * @Description: TODO
 * @date 2018/6/2817:29
 */
@XStreamAlias("Student_Message")
public class Student {
    private String id;
    private String name;
    private String age;
    private String achievement;

    public  Student(String id,String name,String age,String achievement){
        this.id = id;
        this.name = name;
        this.age = age;
        this.achievement = achievement;
    }
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getAchievement() {
        return achievement;
    }

    public void setAchievement(String achievement) {
        this.achievement = achievement;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=‘" + id + ‘\‘‘ +
                ", name=‘" + name + ‘\‘‘ +
                ", age=‘" + age + ‘\‘‘ +
                ", achievement=‘" + achievement + ‘\‘‘ +
                ‘}‘;
    }
}

工具类

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author ceshi
 * @Title: XStreamUtils
 * @ProjectName ceshi
 * @Description: TODO
 * @date 2018/6/2817:38
 */
public class XStreamUtils {
    /**
     * 将Object转换为xml
     * @param obj 转换的bean
     * @return bean转换为xml
     */
    public static String objectToXml(Object obj) {
        XStream xStream = new XStream();
        //xstream使用注解转换
        xStream.processAnnotations(obj.getClass());
        return xStream.toXML(obj);
    }

    /**
     * 将xml转换为T
     * @param <T> 泛型
     * @param xml 要转换为T的xml
     * @param cls T对应的Class
     * @return xml转换为T
     */
    public static <T> T xmlToObject(String xml, Class<T> cls){
        XStream xstream = new XStream(new DomDriver());
        //xstream使用注解转换
        xstream.processAnnotations(cls);
        return (T) xstream.fromXML(xml);
    }
}

测试类

import org.junit.Test;

/**
 * @author ceshi
 * @Title: JunitXStream
 * @ProjectName ceshi
 * @Description: TODO
 * @date 2018/6/2817:29
 */
public class JunitXStream {
    @Test
    public void test(){
        Student s = new Student("1","张三","18","90");
        String xml = XStreamUtils.objectToXml(s);
        xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+xml;
        xml = xml.replaceAll("__","_");
        System.out.println(xml);

        Student ss = XStreamUtils.xmlToObject(xml,Student.class);
        System.out.println(ss);
    }
}

结果:

技术分享图片

pom:需要使用到  xstream-1.4.8.jar  

<dependency>
  <groupId>com.thoughtworks.xstream</groupId>
   <artifactId>xstream</artifactId>
   <version>1.4.8</version>
</dependency>

注意事项:

  1.XStream有一些小bug,在定义别名中的下划线“_”转换为xml后会变成“__”这个符号,下划线问题可以用下面这个解决

    XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_")));

    也可以像测试类中用:replaceAll("__","_"); 替换掉

 

 

java与xml转换 -- XStreamAlias

标签:ati   注解   import   tox   持久   mes   version   pom   tag   

原文地址:https://www.cnblogs.com/qinxu/p/9240038.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!