Home >Backend Development >XML/RSS Tutorial >A powerful tool for converting JAVAbean and XML - Detailed code explanation of XStream
XStream is an open source project under the famous thought works. Its main function is to provide conversion between Java beans and XML text. It also provides JAVA beans and The 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. Process attributes;
4. Process listType 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:
<xstreamTest.Person> <name>pli</name> <age>18</age> </xstreamTest.Person>
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:
<person> <name>pli</name> <age>18</age> </person>
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:
<person age="18"> <name>pli</name> </person>
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<String> girlFriends; public List<String> getGirlFriends() { return girlFriends; } public void setGirlFriends(List<String> 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:
<person age="18"> <name>pli</name> <girlFriends> <string>YuanYuanGao</string> <string>QiShu</string> <string>BoZhiZhang</string> </girlFriends> </person>
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 4e4533e6d53566a86c3dd22301ca0fe8 tag and change "98c455a79ddfebb79781bff588e7b37e". Let's take a look at the effect.
@XStreamAlias("person") public class Person { private String name; @XStreamAsAttribute private int age; @XStreamImplicit(itemFieldName="girl") List<String> girlFriends; public List<String> getGirlFriends() { return girlFriends; } public void setGirlFriends(List<String> 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:
<person age="18"> <name>pli</name> <girl>YuanYuanGao</girl> <girl>QiShu</girl> <girl>BoZhiZhang</girl> </person>
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<String> girlFriends; public List<String> getGirlFriends() { return girlFriends; } public void setGirlFriends(List<String> 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!