使用XStream把javaBean转化成xml文件时,当JavaBean的某些变量为null是,将在xml文件中不输出这个属性!如
Class Person{
String name;
String sex;
}
当sex为null时,那么输出的xml文件为
<person>
<name>***</name>
</person>
如果我们希望能把全部变量输出,需要给XStream实例注册一个转化器,实现Converter接口,这个接口提供三个方法,在它的官方文档中有详细介绍。
http://xstream.codehaus.org/javadoc/index.html
这里我们需要实现的功能是,把null值的属性都在xml文件中输出!如上面的Person输出为
<person>
<name>***</name>
<sex></sex>
</person>
那么我们需要新建一个实现Converter接口的类,如
package com.asrfid.jay.action;
import java.lang.reflect.Method;
import java.util.List;
import com.asrfid.jay.dto.Consignee;
import com.asrfid.jay.dto.Consignor;
import com.asrfid.jay.dto.Goods;
import com.asrfid.jay.dto.SendXML;
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;
public class NullConverter implements Converter{
@Override
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
SendXML obj = (SendXML)source;
Method[] methods = SendXML.class.getMethods();
for(Method m : methods){
String methodName = m.getName();
if (methodName.indexOf("get") != -1 && methodName != "getClass") {
String name = methodName.substring(3);
Object o = null;
try {
o = m.invoke(obj, new Object[0]);
} catch (Exception e) {
e.printStackTrace();
}
if (o instanceof Consignee) {
writer.startNode("ConsigneeInfomation");
Consignee consignee = (Consignee)o;
Method[] methods2 = Consignee.class.getMethods();
for (Method m2 : methods2) {
methodName = m2.getName();
if (methodName.indexOf("get") != -1 && methodName != "getClass") {
name = methodName.substring(3);
writer.startNode(name);
Object con1 = null;
try {
con1 = m2.invoke(consignee, new Object[0]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer.setValue(con1==null?"":con1.toString());
writer.endNode();
}
}
writer.endNode();
}else if (o instanceof Consignor) {
writer.startNode("ConsignorInfomation");
Consignor consignor = (Consignor)o;
Method[] methods2 = Consignor.class.getMethods();
for (Method m2 : methods2) {
methodName = m2.getName();
if (methodName.indexOf("get") != -1 && methodName != "getClass") {
name = methodName.substring(3);
writer.startNode(name);
Object con1 = null;
try {
con1 = m2.invoke(consignor, new Object[0]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer.setValue(con1==null?"":con1.toString());
writer.endNode();
}
}
writer.endNode();
}else if(o instanceof java.util.List){
writer.startNode("goodsInformations");
List lists = (java.util.List)o;
for(int i =0;i<lists.size();i++){
Goods goods = (Goods)lists.get(i);
Method[] methods3 = Goods.class.getMethods();
for (Method m3 : methods3) {
methodName = m3.getName();
if (methodName.indexOf("get") != -1 && methodName != "getClass") {
name = methodName.substring(3);
writer.startNode(name);
Object con1 = null;
try {
con1 = m3.invoke(goods, new Object[0]);
} catch (Exception e) {
e.printStackTrace();
}
writer.setValue(con1==null?"":con1.toString());
writer.endNode();
}
}
}
writer.endNode();//goodsInformations
}else {
writer.startNode(name);
writer.setValue(o==null?"":o.toString());
writer.endNode();//
}
}
}
}
@Override
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean canConvert(Class type) {
// TODO Auto-generated method stub
return type.equals(SendXML.class);
}
}
另外我们还需要注册这个类,如
SendXML sendXML = this.sendXMLManager.getSendXML(shippingId);
XStream xStream = new XStream(new DomDriver());
xStream.alias("ShippingOrder", SendXML.class);
xStream.alias("consigneeInfomation", Consignee.class);
xStream.alias("consignorInfomation", Consignor.class);
xStream.alias("GoodsInfomation", Goods.class);
xStream.registerConverter(new com.asrfid.jay.action.NullConverter());
String xml = xStream.toXML(sendXML);