XStreamAlias 能夠把objec和xml相互轉換,可是有時候節點帶有屬性和值就須要特殊處理下:java
<?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>
建立實體類this
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; } }
工具類spa
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); } }
測試類code
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));
} }
結果:xml
XStream使用總結:blog
XStreamAsAttribute 做用是將類內成員做爲父節點屬性輸出,等同於xstream.useAttributeFor(Student.class, "sid")
XStreamAlias("cat") 等同於 xstream.alias("student_Message", Student.class);ip
XStreamConverter xstreamConvert用於指定class及Field的converter(轉換方式)。 ci
XStreamImplicit 註解使用當須要將collection或map類型的成員變量中數據轉換成xml相同層次的元素時,能夠在該成員變量使用該註解,會將添加註釋的節點去掉 @XStreamImplicit(itemFieldName="studentList")