This article describes the an and XML conversion tool classes in detail, which has some reference value. let's take a look at the jar packages using XStream.
X-stream.github.io/index.html
For the jar package, see the attachment.
XStream is a simple library to serialize objects to XML and back again.
Entity class
public class Person { private String firstname; private String lastname; private PhoneNumber phone; private PhoneNumber fax; public Person(String firstname,String lastname){ this.firstname = firstname; this.lastname = lastname; } public String getFirstname() {return firstname;}public void setFirstname(String firstname) {this.firstname = firstname;}public String getLastname() {return lastname;}public void setLastname(String lastname) {this.lastname = lastname;}public PhoneNumber getPhone() {return phone;}public void setPhone(PhoneNumber phone) {this.phone = phone;}public PhoneNumber getFax() {return fax;}public void setFax(PhoneNumber fax) {this.fax = fax;} }public class PhoneNumber {private int code;private String number;public PhoneNumber(int code,String number){this.code = code;this.number = number;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}}
Tool
Import com. thoughtworks. xstream. XStream; import com. thoughtworks. xstream. io. xml. staxDriver; import com. wind. study. entity. person; import com. wind. study. entity. phoneNumber;/***** @ author wind * @ date 4:49:32, January 1, September 13, 2016 * @ Description: bean/XML conversion */public class BeanXMLConvertUtil {public static void main (String [] args) {XStream xstream = new XStream (new StaxDriver (); // XML output of XStream is more concise. you can create an alias for your custom class name X The name of the ML element. This is a unique type of ing that requires XStream or even optional. Xstream. alias ("person", Person. class); xstream. alias ("phonenumber", PhoneNumber. class); Person joe = new Person ("Joe", "Walnes"); joe. setPhone (new PhoneNumber (123, "1234-456"); joe. setFax (new PhoneNumber (123, "9999-999"); // bean to XMLString xml = xstream. toXML (joe); // XML to beanPerson newJoe = (Person) xstream. fromXML (xml); System. out. println (newJoe. getFirstname (); System. out. println (xml );}}
The above section details the an and XML tools. For more information, see other related articles in the first PHP community!