cosmo

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

XStreamAlias 可以把objec和xml相互转换,但是有时候节点带有属性和值就需要特殊处理下:


 

<?xml version="1.0" encoding="UTF-8"?>
<student>
    <studentList>
        <student_Message sid="1">
            <id>1</id>
            <idType name="身份证">1</idType>
            <idNo>1</idNo>
            <name>张三</name>            
            <gender name="男">1</gender>                            
        </student_Message>
        <student_Message id="2">
            <id>2</id>
            <idType name="护照">2</idType>
            <idNo>2</idNo>
            <name>李华</name>            
            <gender name="女">2</gender>                            
        </student_Message>
    </studentList>
</student>    

有时候需要生成或是解析上面这种XML。就需要用到XStream的其他属性

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

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

创建实体类


 

import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.util.List;
/**
 * @author ceshi
 * @Title: StudentList
 * @ProjectName StudentList
 * @Description: TODO
 * @date 2018/7/1122:00
 */
//定义最外节点属性
@XStreamAlias("student")
public class StudentList {
    //根据XML生成student集合
    private List<Student> studentList;
    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }
}
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

/**
 * @author ceshi
 * @Title: Student
 * @ProjectName ceshi
 * @Description: TODO
 * @date 2018/7/1121:54
 */
//定义内部节点
@XStreamAlias("student_Message")
public class Student {
    //定义<student_Message sid="1">属性
    @XStreamAsAttribute()
    private String sid;
    private String id;
    private IdType idType;
    private String idNo;
    private String name;
    private Gender gender;

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getId() {
        return id;
    }

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

    public IdType getIdType() {
        return idType;
    }

    public void setIdType(IdType idType) {
        this.idType = idType;
    }

    public String getIdNo() {
        return idNo;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    public String getName() {
        return name;
    }

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

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }
}
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;

/**
 * @author ceshi
 * @Title: IdType
 * @ProjectName ceshi
 * @Description: TODO
 * @date 2018/7/1121:56
 */
@XStreamAlias("MaxBenefitDurPeriod")
@XStreamConverter(value = ToAttributedValueConverter.class, strings = { "value" })
public class IdType {
    //// 将name作为Cat属性输出在父节点
    @XStreamAsAttribute()
    private String name;
    private String value;

    public String getName() {
        return name;
    }

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

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;

/**
 * @author ceshi
 * @Title: Gender
 * @ProjectName ceshi
 * @Description: TODO
 * @date 2018/7/1121:58
 */
@XStreamAlias("MaxBenefitDurPeriod")
@XStreamConverter(value = ToAttributedValueConverter.class, strings = { "value" })
public class Gender {
    @XStreamAsAttribute()
    private String name;
    private String value;

    public String getName() {
        return name;
    }

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

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

工具类


 

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

/**
 * @author ceshi
 * @Title: XStreamUtils
 * @ProjectName ceshi
 * @Description: TODO
 * @date 2018/7/1122:10
 */
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;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ceshi
 * @Title: ceshi
 * @ProjectName ceshi
 * @Description: ceshiXStreamAlias
 * @date 2018/7/1121:53
 */
public class JunitXStreamAlias {
    @Test
    public void test(){
        StudentList studentList = new StudentList();
        List<Student> list = new ArrayList<Student>();
        Student s = new Student();
        IdType i = new IdType();
        Gender g = new Gender();
        s.setSid("1");
        s.setId("1");
        i.setName("身份证");
        i.setValue("1");
        s.setIdType(i);
        s.setIdNo("1");
        s.setName("张三");
        g.setName("男");
        g.setValue("1");
        s.setGender(g);
        list.add(s);
        Student s1 = new Student();
        IdType i1 = new IdType();
        Gender g1 = new Gender();
        s1.setSid("2");
        s1.setId("2");
        i1.setName("护照");
        i1.setValue("2");
        s1.setIdType(i1);
        s1.setIdNo("2");
        s1.setName("李华");
        g1.setName("女");
        g1.setValue("2");
        s1.setGender(g1);
        list.add(s1);
        studentList.setStudentList(list);
        String xml = XStreamUtils.objectToXml(studentList);
        xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+xml;
        xml = xml.replaceAll("__","_");
        System.out.println(xml);
     StudentList ss = XStreamUtils.xmlToObject(xml,StudentList.class);
     System.out.println(JSON.toJSON(ss));
  } }

结果:


 

 

XStream使用总结:


 

  XStreamAsAttribute 作用是将类内成员作为父节点属性输出,等同于xstream.useAttributeFor(Student.class, "sid")  
  XStreamAlias("cat") 等同于 xstream.alias("student_Message", Student.class);

  XStreamConverter xstreamConvert用于指定class及Field的converter(转换方式)。

  XStreamImplicit 注解使用当需要将collection或map类型的成员变量中数据转换成xml相同层次的元素时,可以在该成员变量使用该注解,会将添加注释的节点去掉 @XStreamImplicit(itemFieldName="studentList")

 

posted on 2018-07-11 22:44  【cosmo】  阅读(11662)  评论(0编辑  收藏  举报