Java -- XStreamAlias 處理節點中的屬性和值


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")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM