II. important annotations @ XStreamAlias alias @ XStreamAsAttribute is defined as attribute @ XStreamOmitField ignore @ XStreamConverter processing date format @ XStreamImplicit (itemFieldName & quot; roles & quot ;) process List 1. import jar packages
com.thoughtworks.xstream
xstream
1.4.8
II. Important Notes
@ XStreamAlias defines the alias
@ XStreamAsAttribute is defined as an attribute.
@ XStreamOmitField ignore
@ XStreamConverter processing date format
@ XStreamImplicit (itemFieldName = "roles") process List
III. instances
1. define JavaBean
import java.util.Date;import java.util.List;import com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.annotations.XStreamAsAttribute;import com.thoughtworks.xstream.annotations.XStreamConverter;import com.thoughtworks.xstream.annotations.XStreamImplicit;import com.thoughtworks.xstream.annotations.XStreamOmitField;import com.tzz.util.xml.DateConverter;@XStreamAlias("user")public class User {@XStreamAsAttributeprivate Integer id;private String name;@XStreamOmitFieldprivate int age;private String sex;@XStreamConverter(value = DateConverter.class)private Date birthday = new Date();@XStreamImplicit(itemFieldName = "roles")private List
roles;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public List
getRoles() {return roles;}public void setRoles(List
roles) {this.roles = roles;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}}
import com.thoughtworks.xstream.annotations.XStreamAlias;@XStreamAlias("role")public class Role {private Integer id;private String name;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
2. conversion tool
Import java. io. writer; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import java. util. set; import org. dom4j. element; import com. thoughtworks. xstream. XStream; import com. thoughtworks. xstream. core. util. quickWriter; import com. thoughtworks. xstream. io. hierarchicalStreamWriter; import com. thoughtworks. xstream. io. xml. prettyPrintWriter; import com. thoughtworks. xstream. io. xml. xppDriver; public class XStreamXmlUtil {/** XML to Bean object */@ SuppressWarnings ("unchecked") public static
T xmlToBean (String xml, Class
Clazz) {XStream xstream = new XStream (); xstream. registerConverter (new DateConverter (); xstream. autodetectAnnotations (true); xstream. processAnnotations (clazz); xstream. setClassLoader (clazz. getClassLoader (); return (T) xstream. fromXML (xml);}/** convert Bean Object to XML */public static String beanToXml (Object bean) {// return"
"+ New XStream (). toXML (bean); XStream xstream = new XStream (); xstream. registerConverter (new DateConverter (); xstream. autodetectAnnotations (true); return xstream. toXML (bean );}}
3. test class
Import java. util. arrayList; import java. util. date; import java. util. list; import org. junit. test; import com. tzz. test. util. xml. entity. role; import com. tzz. test. util. xml. entity. user; import com. tzz. util. xml. XStreamXmlUtil; public class TestXStreamXmlUtil {@ Testpublic void testBeanToXml () {try {User user = new User (); user. setId (1); user. setName ("Test"); user. setAge (20); user. setSex ("1"); user. setBirthday (new Date (); Role role = new Role (); role. setId (1); role. setName ("Role 1"); Role role2 = new Role (); role2.setId (2); role2.setName ("Role 2"); List
Roles = new ArrayList
(); Roles. add (role); roles. add (role2); user. setRoles (roles); String xml = XStreamXmlUtil. beanToXml (user); System. out. println (xml);} catch (Exception e) {e. printStackTrace () ;}@ Testpublic void testXmlToBean () {String xml ="
"+"
Test
"+"
1
"+"
16:12:46
"+"
"+"
1
"+"
Role 1
"+"
"+"
"+"
2
"+"
Role 2
"+"
"+"
"; User user = XStreamXmlUtil. xmlToBean (xml, User. class); System. out. println (user. getId () +" -- "+ user. getName (); List
Roles = user. getRoles (); for (Role r: roles) {System. out. println (r. getName ());}}}
4. test results
4.1 run the testBeanToXml method
Test
1
17:35:41
1
Role 1
2
Role 2
4.2 run the testXmlToBean method
1 -- Test role 1 role 2
The above is the details of the code example for XStream to implement mutual conversion between Bean and xml. For more information, see other related articles in the first PHP community!