Detailed XStream Aliases

Source: Internet
Author: User

In the previous section, we've seen how easy it is to use XStream, and this article will continue to explain XStream aliases in an instance-by-case manner. After all, your Java object may not always be as good as the XML structure, and no one is sure that a developer's hand has mistakenly hit the wrong letter, or that the name of the message has changed.


Suppose we output the following XML structure, what should we do?

<blog author= "a Papaya" > <entry> <title> First </title> <description> Welcome to the Papaya Blog! </description> </entry> <entry> <title> second </title> <description> nationwide start anti-fog Haze Red Warning. </description> </entry> </entries></blog>


1.   Building Java objects based on XML structure

package com.favccxx.favsoft.pojo;import java.util.arraylist;import java.util.list;public  Class blog {    private author writer;    private  List<Entry> entries = new ArrayList<Entry> ();     Public blog (Author writer)  {        this.writer =  writer;    }    public void add (Entry entry)  {        entries.add (Entry);    }     public void addentry (entry entry) {         entries.add (Entry);    }    public list<entry>  GetContent ()  {        return entries;     }    puBlic author getwriter ()  {        return writer;     }    public void setwriter (Author writer)  {         this.writer = writer;    }}


package com.favccxx.favsoft.pojo;public class entry {    private  String title;    private string description;    public  entry (string title, string description)  {         this.title = title;        this.description =  description;    }    public string gettitle ()  {         return title;    }     public void settitle (String title)  {         this.title = title;    }    public string  GetDescription ()  {        return description;     }     public void setdescription (string description)  {         this.description = description;    }}


2. start the Code test

package com.favccxx.favsoft.main;import  com.favccxx.favsoft.pojo.author;import com.favccxx.favsoft.pojo.blog;import  Com.favccxx.favsoft.pojo.entry;import com.thoughtworks.xstream.xstream;public class mainblog  {    public static void main (string args[])  {         blog myblog = new blog (New Author ("a Papaya"));         myblog.add (New entry ("The first article",  "Welcome to the Papaya Blog! ");         myblog.add (New entry (" The second ", " national launch of anti-fog haze red warning. "));         xstream xstream = new xstream ();         system.out.println (Xstream.toxml (MyBlog));     }} 


3. What do you do if you find that the output structure is not what you imagined?

<com.favccxx.favsoft.pojo.Blog> <writer> <name> A papaya </name> </writer> <entries> <com.favccxx.favsoft.pojo.Entry> <title> First </title> <description> Welcome to Papaya Blog! </description> </com.favccxx.favsoft.pojo.Entry> <com.favccxx.favsoft.pojo.Entry> <TITLE&G t; the second </title> <description> National launch of anti-fog haze red warning. </description> </com.favccxx.favsoft.pojo.Entry> </entries></com.favccxx.favsoft.pojo.blog >


4. Use the category name to replace the class object that contains the path.

Xstream.alias ("blog", Blog.class); Xstream.alias ("entry", Entry.class);


It is found that the output structure is a little closer to the desired structure, but still does not meet the requirements.

<blog> <writer> <name> A papaya </name> </writer> <entries> <entry> < Title> First </title> <description> Welcome to Papaya Blog! </description> </entry> <entry> <title> second </title> <description> nationwide start anti-fog Haze Red Warning. </description> </entry> </entries></blog>


5. Using the field alias , the writer writer author to the author, found that the output structure is close to one layer.

Xstream.aliasfield ("Author", Blog.class, "writer");


<blog> <author> <name> A papaya </name> </author> <entries> <entry> < Title> First </title> <description> Welcome to Papaya Blog! </description> </entry> <entry> <title> second </title> <description> nationwide start anti-fog Haze Red Warning. </description> </entry> </entries></blog>


6. Use implicit collections to hide the root nodes of collections that do not need to be presented. It is important to note that arrays and map combinations cannot be declared as implicit collections.

Xstream.addimplicitcollection (Blog.class, "entries");


<blog> <author> <name> A papaya </name> </author> <entry> <title> first article </titl E> <description> Welcome to Papaya Blog! </description> </entry> <entry> <title> second </title> <description> National launch of anti-fog red alert. </description> </entry></blog>


7. Use the attribute alias to present the member object in the XML as a property label. But the attribute tag does not go directly to the XML tag, it needs to implement the singlevalueconverter converter.

Xstream.useattributefor (Blog.class, "writer"), Xstream.aliasfield ("Author", Blog.class, "writer");


Package Com.favccxx.favsoft.util;import Com.favccxx.favsoft.pojo.author;import Com.thoughtworks.xstream.converters.singlevalueconverter;public class AuthorConverter implements    Singlevalueconverter {@Override public boolean canconvert (Class type) {return type.equals (Author.class);    } @Override Public String toString (Object obj) {return ((Author) obj). GetName ();    } @Override Public Object fromstring (String str) {return new Author (str); }}


8. Finally finished, the final complete code is this

package com.favccxx.favsoft.main;import com.favccxx.favsoft.pojo.author;import  com.favccxx.favsoft.pojo.blog;import com.favccxx.favsoft.pojo.entry;import  com.favccxx.favsoft.util.authorconverter;import com.thoughtworks.xstream.xstream;public class  Mainblog {    public static void main (String args[])  {         blog myblog = new blog (New Author ("one Papaya"));         myblog.add (New entry ("The first article",  "Welcome to the Papaya Blog! ");         myblog.add (New entry (" The second ", " national launch of anti-fog haze red warning. "));         xstream xstream = new xstream ();         xstream.alias ("blog",  blog.class);         xstream.alias ("entry",  entry.class);   &Nbsp;     xstream.useattributefor (blog.class,  "writer");         xstream.aliasfield ("Author", blog.class,  "writer");         xstream.registerconverter (New authorconverter ());         xstream.addimplicitcollection (blog.class,  "entries");         system.out.println (Xstream.toxml (MyBlog));     }}


9. Output a perfect XML structure

<blog author= "a Papaya" > <entry> <title> First </title> <description> Welcome to the Papaya Blog! </description> </entry> <entry> <title> second </title> <description> National launch of anti-fog red alert. </description> </entry></blog>


Sometimes you need to use a package alias to replace the package name. The package name that needs to be replaced must be replaced from the first letter and cannot be found from the middle.

package com.favccxx.favsoft.main;import com.favccxx.favsoft.pojo.author;import  com.favccxx.favsoft.pojo.blog;import com.favccxx.favsoft.pojo.entry;import  com.thoughtworks.xstream.xstream;public class mainblog {    public  Static void main (string args[])  {        blog  myblog = new blog (New author ("a Papaya"));         myblog.add (New entry ("The first article",  "Welcome to the Papaya Blog! ");         myblog.add (New entry (" The second ", " national launch of anti-fog haze red warning. "));         xstream xstream = new xstream ();         xstream.aliaspackage ("My.company",  "Com.favccxx");         system.out.println (Xstream.toxml (MyBlog));     }}


11. The output format is as follows

<my.company.favsoft.pojo.Blog> <writer> <name> A papaya </name> </writer> <entries> <my.company.favsoft.pojo.Entry> <title> First </title> <description> Welcome to Papaya Blog! </description> </my.company.favsoft.pojo.Entry> <my.company.favsoft.pojo.Entry> <title> The second </title> <description> national launch anti-fog haze red warning. </description> </my.company.favsoft.pojo.Entry> </entries></my.company.favsoft.pojo.Blog>


Summary:

    • You can change the name of a label by using the category name

    • You can change the name of a label by using a field alias

    • You can change the name of a label by using a package alias

    • By implementing the Singlevalueconverter interface, you can display the member fields on the Object Properties tab


This article is from the "This person's IT World" blog, be sure to keep this source http://favccxx.blog.51cto.com/2890523/1884457

Detailed XStream Aliases

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.