Solves the problem that XStream does not output labels for null value ToXml

Source: Internet
Author: User

Note: XStream in the toxml for null values do not label output, the following converter modified this content.

There are examples of usage in the main method.


Import Java.lang.reflect.Method; Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;

Import Tellhow.ner.vo.BrakeVo;
Import Tellhow.ner.vo.TransferComponentVo;
Import Tellhow.ner.vo.Unit;

Import Com.thoughtworks.xstream.XStream;
Import Com.thoughtworks.xstream.converters.Converter;
Import Com.thoughtworks.xstream.converters.MarshallingContext;
Import Com.thoughtworks.xstream.converters.UnmarshallingContext;
Import Com.thoughtworks.xstream.io.HierarchicalStreamReader;
Import Com.thoughtworks.xstream.io.HierarchicalStreamWriter;

/**
* Custom XStream Converter,
* Enables null value labels to be exported to XML
*/
public class Nullconverter implements Converter
{
private map<class<?> list<string>> attributes = null;

public void Regattribute (class<?> type, String attribute)
{
if (null = = attributes)
{
attributes = new Hashmap<class<?>, list<string>> ();
}

List value = Attributes.get (type);
if (null = = value)
{
Value = new arraylist<string> ();
Attributes.put (type, value);
}

Value.add (attribute);
}

/**
* Whether it is an attribute (it is a property that is not implemented with a separate label)
* @param type
* @param attribute
* @return
*/
Private Boolean Isclassattribute (class<?> type, String attribute)
{
list<string> value = getattributes (type);
if (null = = value)
return false;
if (Value.contains (attribute))
{
return true;
}
return false;
}

/**
* Get the registered properties
* @param type
* @return
*/
Private list<string> getattributes (class<?> type)
{
if (null!= attributes)
{
return Attributes.get (type);
}
return null;
}

/**
* Output object's property label
* @param source
* @param writer
*/
private void Writerattribute (Object source, hierarchicalstreamwriter writer)
{
Class CType = Source.getclass ();
list<string> value = getattributes (CType);
if ((null!= value) && (value.size () > 0))
{
Method[] methods = Ctype.getmethods ();
For (method Method:methods)
{
String methodname = Method.getname ();
if (Methodname.indexof ("get")!=-1 && methodname!= "GetClass")
{
String name = methodname.substring (3);
Name = Name.tolowercase ();
if (value.contains (name))
{
Object o = null;
try {
o = method.invoke (source, NULL);
catch (Exception e) {
E.printstacktrace ();
}
Writer.addattribute (name, O==null? "": O. ToString ());
}
}
}
}
}

public void Marshal (Object source, hierarchicalstreamwriter writer,
Marshallingcontext context)
{
if (null = = Source)
Return
Class CType = Source.getclass ();
Method[] methods = Ctype.getmethods ();
Writerattribute (source, writer);
For (method M:methods)
{
String methodname = M.getname ();
if (Methodname.indexof ("get")!=-1 && methodname!= "GetClass")
{
if (source instanceof List)
{
List List = (list) source;
for (Object obj:list)
{
String name = Obj.getclass (). toString ();
Name = Name.substring (Name.lastindexof (".") + 1);

Writer.startnode (name);
Marshal (obj, writer, context);
Writer.endnode ();
}
}
Else
{
Boolean isbasetype = Isbasetype (M.getreturntype ());
String name = methodname.substring (3);
if (Isbasetype)
{
Name = Name.tolowercase ();
}
Object o = null;
Try
{
o = m.invoke (source, NULL);
catch (Exception e) {
E.printstacktrace ();
}
If the base type calls ToString, the recursive
if (Isbasetype)
{
if (!isclassattribute (CType, name))
{
Writer.startnode (name);
Writer.setvalue (o==null? "": O. ToString ());
Writer.endnode ();
}
}
Else
{
Writer.startnode (name);
Marshal (O, writer, context);
Writer.endnode ();
}
}
}
}
}

Public Object Unmarshal (Hierarchicalstreamreader Reader,
Unmarshallingcontext context) {
return null;
}

public boolean Canconvert (Class type) {
return true;
}

Private Boolean Isbasetype (class<?> type)
{
if (Type.equals (Integer.class)
|| Type.equals (Double.class)
|| Type.equals (String.class)
|| Type.equals (Boolean.class)
|| Type.equals (Long.class)
|| Type.equals (Short.class)
|| Type.equals (Byte.class)
|| Type.equals (Float.class))
{
return true;
}
return false;
}

public static void Main (string[] args)
{
XStream xs= Exthelper.initxstream ();
Nullconverter v = new Nullconverter ();
V.regattribute (Unit.class, "unittype");
Xs.registerconverter (v);
Unit unit = new Unit ();

Unit.setunitcomment ("Aaaaaaaa1");

list<transfercomponentvo> transfer = new arraylist<transfercomponentvo> ();
Transfer.add (New Transfercomponentvo ());
Unit.settransfercomponentvolist (transfer);

Brakevo brake = new Brakevo ();
Brake.setasetid ("1111111111111100000000001");
Unit.setbrakevo (brake);

String XML = xs.toxml (unit);
SYSTEM.OUT.PRINTLN (XML);
}

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.