JAVAbean and XML conversion tool-XStream code details

Source: Internet
Author: User
XStream is an open-source project under the famous thoughtworks. its main function is to provide conversion between Javabean and XML texts. In addition, it also provides conversion between JAVAbean and JSON, this is not within the scope of this discussion. XStream is an open-source project under the well-known thought works. It provides conversion between Java bean and XML text, and conversion between JAVA bean and JSON, this is not within the scope of this discussion.
After JAVA1.5, XSteam also supports annotation. In this case, you only need to add several annotations to the java bean. of course, if you cannot modify the JAVA bean, XStream also provides the register method, which is also very simple. The following are some details:
1. basic conversion;
2. use an alias;
3. process attributes;
4. process list type attributes;
5. attributes are not involved in conversion;

1. basic conversion
This is a common 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 as follows:

XStream xstream = new XStream();  Person person = new Person();  person.setName("pli");  person.setAge(18);  System.out.println(xstream.toXML(person));

We get the following result:

     
  
   pli
      18  
 

But sometimes the root label does not want to use the package path. how can this problem be solved by using the alias?

2. Alias
Jia Ding, we want to change the inexplicable element label "xstreamTest. Person" to "person". we should do this.

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));

In this way, we get the desired:

     
  
   pli
      18  
 

3. process attributes
What should I do if I want to use the "age" attribute in JAVA bean as an attribute of the person tag in XML.
Here we will introduce 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 as follows:

     
  
   pli
    
 

4. process List
What is the case if List exists in JAVA bean.

@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;      }  }
   
  
 

The following result is returned for direct conversion:

     
  
   pli
      
        
   
    YuanYuanGao
         
   
    QiShu
         
   
    BoZhiZhang
       
    
 

XStream provides an annotation @ XStreamImplicit (itemFieldName = ***) to satisfy the user's need to remove the root node of the List and change the List name, corresponding to our example is removed Tag and change" ". Let's see 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;      }  }
   
  
 

The result is as follows:

     
  
   pli
      
  
   YuanYuanGao
      
  
   QiShu
      
  
   BoZhiZhang
    
 

5. ignore attributes
If some attributes in JAVA bean do not want to be serialized, XStream provides annotation: @ XStreamOmitField
For example, you don't want to talk about the List serialization of girlfriends.

@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;      }  }
   
  
 

The above is the detailed explanation of XStream code, a powerful tool for converting JAVAbean and XML. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.