JAVA bean and XML interconversion tool---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, and also provide conversion between JAVA beans and JSON, which is not within the scope of this discussion.
After JAVA1.5, XSteam also supports annotation. At this time, you only need to add a few annotations to the JAVA BEAN. Of course, if the JAVA bean is not allowed to be modified, then XStream also provides a register method, which is also very simple. It will be explained in detail through several aspects:
1. Basic conversion;
2. Using aliases;
3. Handling attributes;
4. Handling list type attributes;
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 this:
XStream xstream = new XStream(); 
Person person = new Person(); 
person.setName("pli") ; 
person.setAge(18); 
System.out.println(xstream.toXML(person)); 

we get this result:
<xstreamTest.Person> 
  <name>pli</name> 
  <age>18</age> 
</xstreamTest.Person> 
But sometimes the root tag doesn't want to use the package path, how to do it, use an alias

2. Create an
alias We want to change the inexplicable element tag "xstreamTest.Person" to "person" We should do this.

package xstreamTest; 
@XStreamAlias("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; 
    } 
}

and 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. Handling attributes
What if you want to use the "age" attribute in the Java bean as an attribute of the person tag in XML Woolen cloth.
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; 
    } 


结果是这样的:

<person age="18"> 
  <name>pli</name> 
</person>

4. 处理List
如果JAVA bean中有List是什么情形呢。

@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 get this Result:
<person age="18"> 
  <name>pli</name> 
  <girlFriends> 
    <string>YuanYuanGao</string> 
    <string>QiShu</string> 
    <string>BoZhiZhang</string> 
  </girlFriends> 
< /person>

XStream provides a @XStreamImplicit(itemFieldName=***) annotation here to meet the needs of users who want to remove the root node of the List and change the name of the list, which corresponds to our example is to remove <girlFriends> tag and change "<string>". Let's see 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 this:

<person age="18"> 
  <name>pli</name> 
  <girl>YuanYuanGao</girl> 
  <girl >QiShu</girl> 
  <girl>BoZhiZhang</girl> 
</person> 

5. Ignore properties
If there are some properties in the JAVA bean that do not want to be serialized, XStream provides an annotation to solve this requirement: @XStreamOmitField,
for example, do not want to talk about girlfriends This List serializes

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

结果是这样:

<person age="18"> 
  <name>pli</name> 
</person>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326396293&siteId=291194637