Java -- XStreamAlias 处理节点中的属性和值
XStreamAlias 可以把objec和xml相互轉(zhuǎn)換,但是有時候節(jié)點(diǎn)帶有屬性和值就需要特殊處理下:
?
<?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="護(hù)照">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>創(chuàng)建實(shí)體類
?
import com.thoughtworks.xstream.annotations.XStreamAlias; import java.util.List; /*** @author ceshi* @Title: StudentList* @ProjectName StudentList* @Description: TODO* @date 2018/7/1122:00*/ //定義最外節(jié)點(diǎn)屬性 @XStreamAlias("student") public class StudentList {//根據(jù)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*/ //定義內(nèi)部節(jié)點(diǎn) @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屬性輸出在父節(jié)點(diǎn) @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轉(zhuǎn)換為xml* @param obj 轉(zhuǎn)換的bean* @return bean轉(zhuǎn)換為xml*/public static String objectToXml(Object obj) {XStream xStream = new XStream();//xstream使用注解轉(zhuǎn)換 xStream.processAnnotations(obj.getClass());return xStream.toXML(obj);}/*** 將xml轉(zhuǎn)換為T* @param <T> 泛型* @param xml 要轉(zhuǎn)換為T的xml* @param cls T對應(yīng)的Class* @return xml轉(zhuǎn)換為T*/public static <T> T xmlToObject(String xml, Class<T> cls){XStream xstream = new XStream(new DomDriver());//xstream使用注解轉(zhuǎn)換 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 {@Testpublic 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("護(hù)照");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)); } }
結(jié)果:
?
?
XStream使用總結(jié):
?
XStreamAsAttribute 作用是將類內(nèi)成員作為父節(jié)點(diǎn)屬性輸出,等同于xstream.useAttributeFor(Student.class, "sid")
XStreamAlias("cat") 等同于 xstream.alias("student_Message", Student.class);
XStreamConverter xstreamConvert用于指定class及Field的converter(轉(zhuǎn)換方式)。
XStreamImplicit 注解使用當(dāng)需要將collection或map類型的成員變量中數(shù)據(jù)轉(zhuǎn)換成xml相同層次的元素時,可以在該成員變量使用該注解,會將添加注釋的節(jié)點(diǎn)去掉?@XStreamImplicit(itemFieldName="studentList")
?
posted on 2018-07-11 22:44 【cosmo】 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/qinxu/p/9297099.html
總結(jié)
以上是生活随笔為你收集整理的Java -- XStreamAlias 处理节点中的属性和值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android ListView性能优化
- 下一篇: java美元兑换,(Java实现) 美元