XStream is an open source project under the famous thought works. Its main function is to provide conversion between Java beans andXMLtext. It also provides JAVA beans andThe conversion between JSON is outside the scope of this discussion.
XSteam also supports annotation after JAVA1.5. At this time, you only need to add some annotations to the JAVA BEAN. Of course, if the JAVA bean is not allowed to be modified, XStream also provides a register method, which is also very simple. Let’s explain in detail through several aspects:
1. Basic conversion;
2. Use aliases;
3. Processattributes;
4. ProcesslistType attribute;
5. Attributes do not participate in conversion;
1. Basic conversion
This is an ordinary JAVA bean:
package xstreamTest; public class Person { private String name; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } }
The conversion code is like this:
XStream xstream = new XStream(); Person person = new Person(); person.setName("pli"); person.setAge(18); System.out.println(xstream.toXML(person));
We got this result:
pli 18
But sometimes the root tag does not want to use the package path, how to do it, use an alias
2. Make an alias
Jia Ding us We hope to change the inexplicable element tag "xstreamTest.Person" to "person". This is what we should do.
package xstreamTest; @XStreamAlias("person") public class Person { private String name; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } }
The execution code will become like this:
XStream xstream = new XStream(); xstream.autodetectAnnotations(true); Person person = new Person(); person.setName("pli"); person.setAge(18); System.out.println(xstream.toXML(person));
So we get what we want:
pli 18
3. Processing attributes
If you want to use JAVA What should we do if the "age" attribute in the bean is used as an attribute of the person tag in XML.
Here is another annotation: @XStreamAsAttribute, our JAVA bean becomes like this:
@XStreamAlias("person") public class Person { private String name; @XStreamAsAttribute private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } }
The result is like this:
pli
4. Processing List
If JAVA bean What is the situation when there is a List in .
@XStreamAlias("person") public class Person { private String name; @XStreamAsAttribute private int age; ListgirlFriends; public List getGirlFriends() { return girlFriends; } public void setGirlFriends(List girlFriends) { this.girlFriends = girlFriends; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } }
Direct conversion we will get this result:
pli YuanYuanGao QiShu BoZhiZhang
XStream provides an @XStreamImplicit(itemFieldName=***) annotation here to satisfy the user who wants to remove the root node of the List And the need to change the name of the list, corresponding to our example is to remove the
@XStreamAlias("person") public class Person { private String name; @XStreamAsAttribute private int age; @XStreamImplicit(itemFieldName="girl") ListgirlFriends; public List getGirlFriends() { return girlFriends; } public void setGirlFriends(List girlFriends) { this.girlFriends = girlFriends; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } }
The result is like this:
pli YuanYuanGao QiShu BoZhiZhang
5. Ignore attributes
If there are some attributes in the JAVA bean that you do not want to be serialized, XStream provides an annotation to solve this requirement: @XStreamOmitField
For example, I don’t want to talk about the serialization of the girlfriends list
@XStreamAlias("person") public class Person { private String name; @XStreamAsAttribute private int age; @XStreamImplicit(itemFieldName="girl") @XStreamOmitField ListgirlFriends; public List getGirlFriends() { return girlFriends; } public void setGirlFriends(List girlFriends) { this.girlFriends = girlFriends; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } }
The above is the detailed content of A powerful tool for converting JAVAbean and XML - Detailed code explanation of XStream. For more information, please follow other related articles on the PHP Chinese website!