XStream implements a free transition between XML and Object

Source: Internet
Author: User

    • Because the XML interaction data was used in the project, I thought of the tool (XStream) for a time, so I figured out how to use it. Through some of its own programming experience and the strong network, the ultimate easy to get done. You need to prepare the following jar packages (Xstream-1.4.7.jar, Xpp3_min-1.1.4c.jar, Xmlpull-1.1.3.1.jar)

      1. Writing the corresponding entity

    • Rootbean

    @XStreamAlias("root")    publicclass RootBean {        @XStreamAlias("DocInfo")        private DocInfoBean docInfo;        @XStreamAlias("PAGES")        private List<PageBean> pages;        //getter和setter        ...    }
    • Docinfobean
publicclass DocInfoBean {    @XStreamAlias("APP_CODE")    private String appCode;    @XStreamAlias("APP_NAME")    private String appName;    @XStreamAlias("BUSI_NO")    private String busiNo;    //getter和setter        ...}
    • Pagebean
@XStreamAlias("Page") Public  class pagebean {    @XStreamAlias("Doc_type")@XStreamAsAttribute    PrivateString DocType;@XStreamAlias("Doc_name")@XStreamAsAttribute    PrivateString DocName;@XStreamAlias("Page_url")@XStreamAsAttribute    PrivateString Pageurl;@XStreamAlias("Page_name")@XStreamAsAttribute    PrivateString PageName;@XStreamAlias("Up_user")@XStreamAsAttribute    PrivateString Upuser;@XStreamAlias("up_org")@XStreamAsAttribute    PrivateString uporg;@XStreamAlias("Up_time")@XStreamAsAttribute    PrivateString UpTime;//getter and Setter...}

2. Xml to Object

PrivateRootbeanGetrootbean(File xmlfile) {InputStreamReader reader =NULL;Try{reader =NewInputStreamReader (NewFileInputStream (Busixmlfile),"UTF-8"); }Catch(Exception e)        {E.printstacktrace (); } XStream xstream=NewXStream (NewDomdriver ("UTF-8",NewXmlfriendlynamecoder ("-_","_"))); Xstream.alias ("Root", Rootbean.class); Xstream.autodetectannotations (true);return(Rootbean) xstream.fromxml (reader); }

2. Object to XML

public void Testobj2xmlnew () {Rootbean Rootbean = new Rootbean ();Docinfobean docinfo = new Docinfobean ();DocInfo. Setappcode("009");DocInfo. Setappname("Test");DocInfo. Setbusino("12345a");Rootbean. Setdocinfo(DocInfo);list<pagebean> pages = new arraylist<pagebean> ();for (int i=0; i<3;i++) {Pagebean page = new Pagebean ();Page. Setdocname("ID card");Page. Setdoctype("010");Page. Setpagename("0000.jpg");Page. Setpageurl("1111.jpg");Page. setuporg("HP");Page. Setuptime("34543312321");Page. Setupuser("12433124");Pages. Add(page);} Rootbean. Setpages(pages);Resolves an earlier bug ("_"When you convert to XML, it becomes"__")//xstream xstream=new XStream (New Domdriver ()); XStream xstream=new XStream (New Domdriver ("UTF-8", New Xmlfriendlynamecoder ("-_","_")));XStream. Alias("Root", Rootbean. Class);XStream. Autodetectannotations(true);String str= XStream. ToXML(Rootbean);System. out. println(str);}
    • Run results
<root>  <docinfo>    <APP_CODE>009</APP_CODE>    <app_name>Test</app_name>    <busi_no>12345a</busi_no>  </docinfo>  <PAGES>    <page doc_type="010" doc_name= "ID " page_url=" 1111.jpg " page_name=" 0000.jpg " up_user=" 12433124 " up_org ="HP" up_time="34543312321"/>    <page  DOC_TYPE  =< Span class= "Hljs-value" > "010"  doc_name  = "id"  page_url  = "1111.jpg"  page_name  =" 0000.jpg " up_ USER  = "12433124"  up_org  =" HP " up_time  =" 34543312321 " />     <page doc_type="010" doc_name= "ID " page_url=" 1111.jpg " page_name=" 0000.jpg " up_user=" 12433124 " up_org= "HP" up_time="34543312321"/>  </PAGES></root>

XStream implements a free transition between XML and Object

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.