Conversion between simple Java classes and XML and javaxml

Source: Internet
Author: User

Conversion between simple Java classes and XML and javaxml

  

Jar package: xmlpull_1_0_5.jar, xstream-1.4.1.jar)

1. Tool class XstreamUtil

  PackageCom. learn. util;

ImportCom. thoughtworks. xstream. XStream;

ImportCom. thoughtworks. xstream. io. xml. DomDriver;

/**

*

*@ ClassName: XstreamUtil. java

*@ Description:

*@ Author: Fengzf fengzf@gstarcad.com

*@ Date: March December 6, 2016

*

*/

PublicClassXstreamUtil {

/**

* Convert bean to xml

*@ ParamObj-converted bean

*@ ReturnConvert bean to xml

*/

PublicStaticString objectToXml (Object obj ){

XStream xStream =NewXStream ();

// Use annotation conversion for xstream

XStream. processAnnotations (obj. getClass ());

ReturnXStream. toXML (obj );

}

/**

* Convert xml to bean

*@ Param<T> generic

*@ ParamXml to be converted to bean xml

*@ ParamClass corresponding to cls bean

*@ ReturnConvert xml to bean

*/

PublicStatic<T> T xmlToObject (String xml, Class <T> cls ){

XStream xstream =NewXStream (NewDomDriver ());

// Use annotation conversion for xstream

Xstream. processAnnotations (cls );

Return(T) xstream. fromXML (xml );

}

}

2. Convert Java common object classes to xml

Object Class Message. java code

  PackageCom. learn. entity;

ImportCom. thoughtworks. xstream. annotations. XStreamAlias;

 

@ XStreamAlias ("Response ")

PublicClassMessage {

 

@ XStreamAlias ("returncode ")

PrivateString returnCode;

 

@ XStreamAlias ("returnmessage ")

PrivateString returnMessage;

 

@ XStreamAlias ("status ")

PrivateIntStatus;

 

@ XStreamAlias ("time ")

PrivateString reqTime;

 

PublicString getReturnCode (){

ReturnReturnCode;

}

 

PublicVoidSetReturnCode (String returnCode ){

This. ReturnCode = returnCode;

}

PublicString getReturnMessage (){

ReturnReturnMessage;

}

PublicVoidSetReturnMessage (String returnMessage ){

This. ReturnMessage = returnMessage;

}

PublicIntGetStatus (){

ReturnStatus;

}

PublicVoidSetStatus (IntStatus ){

This. Status = status;

}

PublicString getReqTime (){

ReturnReqTime;

}

PublicVoidSetReqTime (String reqTime ){

This. ReqTime = reqTime;

}

}

 

3. convert an XML string to an object

@ XStreamAlias

I. features:

Simplified API;
No ing file;
High performance, low memory usage;
Neat XML;
Objects do not need to be modified; private fields are supported, and the setter/getter method is not required.
Provides serialization interfaces;
Custom Conversion Type policy;

II. Application scenarios

Transport Conversion
Persistence object
Configuration
Unit Tests Unit test

XML message interaction between Interface services

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.