Resolving Object-to-XML text collection node duplication problem based on XStream tool

Source: Internet
Author: User

http://blog.csdn.net/cqstart116/article/details/44495979
Following the link to the blog, the specific problem of the solution

    • Example of an effect XML
<?xml version= "1.0" encoding= "Utf-8"?><root>    <base_data>        <user_code>0001</user_code>        <user_name>0001</user_name>        <org_code>010</org_code>        <com_code>010</com_code>        <org_name>010</org_name>        <role_code>1</role_code>    </base_data>      <meta_data>        <image_types>            <image_type>021</image_type>            <image_type>022</image_type>        </image_types>        <BATCH>            <APP_CODE>A21</APP_CODE>            <busi_num>0000**************025</busi_num>        </BATCH>    </meta_data></root>
    • Step1:java Object Writing
  Public  class basedata {    @XStreamAlias("User_code")PrivateString Usercode;@XStreamAlias("user_name")PrivateString UserName;@XStreamAlias("Org_code")PrivateString Orgcode;@XStreamAlias("Com_code")PrivateString Comcode;@XStreamAlias("Org_name")PrivateString OrgName;@XStreamAlias("Role_code")PrivateString Rolecode;//getter and Setter... ...}

Note the difference in this object (how the property imagetypes is handled)

 Public  class MetaData {    @XStreamAlias("BATCH")PrivateBatch Batchnode;@XStreamAlias("Image_types")PrivateList<string> imagetypes =NewArraylist<string> ();@XStreamAlias("Count_per_page")PrivateString Countperpage;///Note that a solution that is converted to an XML node nesting after a duplicate naming    //The node (image_types) is treated as a collection object     Public void Add(String ImageType) { This. Imagetypes.add (ImageType); }//Except attributes: Imagetypes, setter and Getter... ...}
public class Batch{    @XStreamAlias("APP_CODE")    private String appCode;    @XStreamAlias("BUSI_NUM")    //getter及setter    ......}
@XStreamAlias("root")public class Query {    @XStreamAlias("BASE_DATA")    private BaseData baseData;    @XStreamAlias("META_DATA")    private MetaData metaData;    //getter及setter    ......}
    • Java Key Code
    ......XStream XStream=NewXStream (NewDomdriver ("UTF-8",NewXmlfriendlyreplacer ("-_","_"))); XStream.Addimmutabletype (Imagetypebean.Class); XStream.Alias"Image_type",String.Class); XStream.Autodetectannotations (true);StringStr=XStream.ToXML (query);......

Resolving Object-to-XML text collection node duplication problem based on XStream tool

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.