XML and Java Object Transformation--Xstreamalias

Source: Internet
Author: User

@XStreamAlias use

First, Features:

Simplified API;
no mapping file;
High performance, low memory footprint;
Neat XML;
No need to modify objects; Support internal private fields, no Setter/getter method required
provides a serialization interface;
Custom conversion type policy;

Second, the use of the scene

Transport conversion
Persistence persisted objects
Configuration configurations
Unit Tests Units Test

interface interaction of XML messages between services

Case code:

@XStreamAlias ("Kfpt_dept")classDept {PrivateString Deptname; PrivateList<staff>staffs;  PublicString Getdeptname () {returnDeptname; }     Public voidsetdeptname (String deptname) { This. Deptname =Deptname; }     PublicList<staff>getstaffs () {returnstaffs; }     Public voidSetstaffs (list<staff>staffs) {         This. Staffs =staffs; } @Override PublicString toString () {returnTostringbuilder.reflectiontostring ( This, Tostringstyle.multi_line_style); }} @XStreamAlias ("Kfpt_staffs")classStaff {PrivateString name; Private intAge ;  PublicStaff () {Super(); }     PublicStaff (String name,intAge ) {        Super();  This. Name =name;  This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; } @Override PublicString toString () {returnTostringbuilder.reflectiontostring ( This, Tostringstyle.multi_line_style); }}

Conversion Tool Classes:

 Public classXstreamutil {/*** Convert bean to XML *@paramobj-Converted Bean *@returnBean conversion to XML*/     Public StaticString objecttoxml (Object obj) {XStream XStream=NewXStream (); //xstream using annotation transformationsxstream.processannotations (Obj.getclass ()); returnxstream.toxml (obj); }     /*** Convert XML to Bean *@param<T> Generics *@paramXML to be converted to bean XML *@paramCLS Bean corresponding class *@returnconvert XML to Bean*/     Public Static<T> T xmltoobject (String XML, class<t>CLS) {XStream XStream=NewXStream (Newdomdriver ()); //xstream using annotation transformationsxstream.processannotations (CLS); return(T) xstream.fromxml (XML); }}

Cond.............

XML and Java Object Transformation--Xstreamalias

Related Article

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.