A powerful tool for converting JAVAbean and XML - Detailed code explanation of XStream

黄舟
Release: 2017-03-31 14:01:49
Original
1512 people have browsed it

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; } }
Copy after login

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));
Copy after login

We got this result:

 pli 18 
Copy after login

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; } }
Copy after login

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));
Copy after login

So we get what we want:

 pli 18 
Copy after login

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; } }
Copy after login

The result is like this:

 pli 
Copy after login

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; List girlFriends; 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; } }
Copy after login

Direct conversion we will get this result:

 pli  YuanYuanGao QiShu BoZhiZhang  
Copy after login

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 tag and change " ". Let's take a look at the effect.

@XStreamAlias("person") public class Person { private String name; @XStreamAsAttribute private int age; @XStreamImplicit(itemFieldName="girl") List girlFriends; 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; } }
Copy after login

The result is like this:

 pli YuanYuanGao QiShu BoZhiZhang 
Copy after login

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 List girlFriends; 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; } }
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!