The use of XStream's converter

Source: Internet
Author: User

Viewed on the Internet, there are few examples of the use of converter in XStream, here is a simple example of application


The file format is as follows:

<list>
<mapper netype= "2g" >
<sqlByType></sqlByType>
<sqlById></sqlById>
<types>
<include>BTS</include>
</types>
</mapper>
<mapper netype= "3g" >
<types>
<include>NODEB</include>
</types>
</mapper>
</list>


The entity class is defined as follows:

@XStreamAlias ("Mapper")
public class Netypemapper {
@XStreamAsAttribute
Public String Netype;

@XmlElement
Public String Sqlbytype;

@XmlElement
Public String Sqlbyid;

@XStreamAlias ("Types")
Public list<string> includetypes = new arraylist<string> ();
}

Converter:

Static Class Listconverter extends Abstractcollectionconverter {

Public Listconverter (Mapper Mapper) {
Super (mapper);
}

@Override
public boolean Canconvert (Class type) {
Return Type.equals (Arraylist.class);
}

@Override
public void Marshal (Object source, hierarchicalstreamwriter writer,
Marshallingcontext context) {
list<string> list = (list<string>) source;
Listiterator<string> iter = List.listiterator ();
String value = null;
while (Iter.hasnext ()) {
Value = Iter.next ();
Extendedhierarchicalstreamwriterhelper.startnode (writer, "include", List.class);
Writer.setvalue (value);
Writer.endnode ();
}
}

@Override
Public Object Unmarshal (Hierarchicalstreamreader Reader,
Unmarshallingcontext context) {
String nodename = Reader.getnodename ();
System.out.println (nodename);
list<string> list = new arraylist<string> ();
while (Reader.hasmorechildren ()) {
Reader.movedown ();
NodeName = Reader.getnodename ();
System.out.println (nodename+ "-To" +reader.getvalue ());
List.add (Reader.getvalue ());
Reader.moveup ();
}
return list;
}
}

How to use:

XStream xs = new XStream ();
Xs.processannotations (Netypemapper.class);
Xs.registerlocalconverter (Netypemapper.class, "Includetypes",
New Listconverter (
New Defaultmapper (XStream.class.getClassLoader ()));
File File = new file (FilePath); FilePath: Path to File
try {
list<netypemapper> list = (list<netypemapper>) xs
. FromXml (New FileInputStream (file));


This article comes from the "Simple and Easy" blog, so be sure to keep this source http://dengshuangfu.blog.51cto.com/8794650/1638068

The use of XStream's converter

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.