XStream处理null值的问题
使用XStream把javaBean转化成xml文件时,当JavaBean的某些变量为null是,将在xml文件中不输出这个属性!如
Class Person{ String name; String sex;}<person> <name>***</name></person>
<person> <name>***</name> <sex></sex></person>
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{@Overridepublic 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 blocke.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 blocke.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();//}}}}@Overridepublic Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) {// TODO Auto-generated method stubreturn null;}@Overridepublic boolean canConvert(Class type) {// TODO Auto-generated method stubreturn 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); 你好方便留下联系方式么,关于Xstream处理null值的问题有些疑问。QQ:525956272 email:rxcss666@163.com