Java and XML Transformation--Xstreamalias

Source: Internet
Author: User


@XStreamAlias
1. 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;

2. Usage Scenarios

Transport conversion
Persistence persisted objects
Configuration configurations
Unit Tests Units Test
interface interaction of XML messages between services

3.model

Entity class

ImportCom.thoughtworks.xstream.annotations.XStreamAlias;/** * @authorCeshi * @Title: Student * @ProjectName Ceshi * @Description: TODO * @date 2018/6/2817:29*/@XStreamAlias ("Student_message") Public classStudent {PrivateString ID; PrivateString name; PrivateString age; PrivateString achievement;  PublicStudent (String id,string name,string age,string achievement) { This. ID =ID;  This. Name =name;  This. Age =Age ;  This. Achievement =achievement; }     PublicString getId () {returnID; }     Public voidsetId (String id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString getage () {returnAge ; }     Public voidsetage (String age) { This. Age =Age ; }     PublicString getachievement () {returnachievement; }     Public voidsetachievement (String achievement) { This. Achievement =achievement; } @Override PublicString toString () {return"student{" + "id=" + ID + ' \ ' + ', name= ' + name + ' \ ' + ', age= ' "+ Age + ' \ ' + ', achievement= ' + achievement + ' \ ' + '} '; }}

Tool class

ImportCom.thoughtworks.xstream.XStream;ImportCom.thoughtworks.xstream.io.xml.DomDriver;/** * @authorCeshi * @Title: Xstreamutils * @ProjectName Ceshi * @Description: TODO * @date 2018/6/2817:38*/ Public classXstreamutils {/*** Convert object 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 T *@param<T> Generics *@paramXML to be converted to t XML *@paramCLS T corresponds to class *@returnconvert XML to T*/     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); }}

Test class

Importorg.junit.Test;/** * @authorCeshi * @Title: Junitxstream * @ProjectName Ceshi * @Description: TODO * @date 2018/6/2817:29*/ Public classJunitxstream {@Test Public voidTest () {Student s=NewStudent ("1", "Zhang San", "18", "90"); String XML=Xstreamutils.objecttoxml (s); XML= "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>" +XML; XML= Xml.replaceall ("__", "_");        SYSTEM.OUT.PRINTLN (XML); Student SS= Xstreamutils.xmltoobject (xml,student.class);    SYSTEM.OUT.PRINTLN (ss); }}

Results:

POM: Need to use to Xstream-1.4.8.jar

<Dependency>    <groupId>Com.thoughtworks.xstream</groupId>   <Artifactid>XStream</Artifactid>   <version>1.4.8</version></Dependency>

Precautions:

1.XStream There are some small bugs, in the definition of the alias in the underscore "_" after the conversion to XML will become "__" This symbol, the underscore problem can be resolved with the following

XStream XStream = new XStream (new Xppdriver (New Xmlfriendlynamecoder ("_-", "_"));

It can also be used as a test class: ReplaceAll ("__", "_"); Replace

Java and XML Transformation--Xstreamalias

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.