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);
}