Use of JAVA XStream

Source: Internet
Author: User

Mavn Package:
<Dependency>
<groupId>Com.thoughtworks.xstream</groupId>
<Artifactid>XStream</Artifactid>
<version>1.4.9</version>
</Dependency>

Filtering Unrecognized nodes
 PackageCommon

ImportCom.thoughtworks.xstream.XStream;
ImportCom.thoughtworks.xstream.mapper.MapperWrapper;

Public classEtlxstreamextendsXStream {
/*
* (Non-javadoc)
*
* @see
* Com.thoughtworks.xstream.xstream#wrapmapper
*/
@Override
protectedMapperwrapper Wrapmapper (Mapperwrapper next) {
return newMapperwrapper (Next) {
/*
* (Non-javadoc)
*
* @see
* Com.thoughtworks.xstream.mapper.mapperwrapper#shouldserializemember
*/
@Override
Public BooleanShouldserializemember (@SuppressWarnings("Rawtypes") Class Definedin, String fieldName) {
//Unrecognized node, skimmed.
if(Definedin = = Object.class){
Try{
return This. Realclass (fieldName)! =NULL;
}Catch(Exception e) {
return False;
}
}Else{
return Super. Shouldserializemember (Definedin, fieldName);
}
}
};
}
}
Xmlhelper Package Class
 PackageCommon

ImportJava.io.InputStream;
ImportJava.lang.Class;
ImportJava.util.HashMap;
ImportJava.util.Map;
ImportJava.util.Map.Entry;

Public classXmlhelper {
Public Static<T> String ToXml (TObject, hashmap<string, class> map) {
Etlxstream XStream =Getxstream(map);
for(Entry<string, Class> entry:map.entrySet ()) {
Xstream.alias (Entry.getkey (), Entry.getvalue ());
}
returnXstream.toxml (object);
}

Public Static<T>TToobject (String XML, hashmap<string, class> map) {
Etlxstream XStream =Getxstream(map);
TObject = (T) xstream.fromxml (XML);
returnObject
}

Public Static<T>TToobject (InputStream stream, hashmap<string, class> map) {
Etlxstream XStream =Getxstream(map);
TObject = (T) Xstream.fromxml (stream);
returnObject
}

private staticEtlxstream Getxstream (hashmap<string, class> map) {
Etlxstream XStream =NewEtlxstream ();
for(Entry<string, Class> entry:map.entrySet ()) {
Xstream.alias (Entry.getkey (), Entry.getvalue ());
}
returnXStream
}
}

Use of JAVA XStream

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.