java与xml转换 -- XStreamAlias

Posted cosmo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java与xml转换 -- XStreamAlias相关的知识,希望对你有一定的参考价值。


@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的主要内容,如果未能解决你的问题,请参考以下文章

Java&amp;Xml教程JAXB实现XML与Java对象转换

java与xml转换 -- XStreamAlias

JAXB实现java对象与xml之间转换

Java中xml与json的相互转换

xml与java对象转换 -- XStreamAlias

JAVA 实现XML与JSON 相互转换