一篇解答xstream转换过程中可能出现问题的作文

时间:2021-09-22 09:20:26

转自http://am-human.iteye.com/blog/1496520

官网:http://xstream.codehaus.org/index.html 

使用中需要注意的问题: 
1.java对象的字段如果有下划线xstream转成xml时会变成双下划线,解决办法: 

Java代码  一篇解答xstream转换过程中可能出现问题的作文
  1. XStream xs = new XStream(new XppDriver(new XmlFriendlyReplacer("__""_")));  

2.xstream默认的时间串格式较少,在默认不满足的情况下,需要自己实现Converter接口,并注册到xstream中 

3.在java对象比较复杂的情况下,为了代码组织方便,使用静态内部类是个好办法。注:使用普通内部类转换后的xml会生成依赖声明 

Java代码  一篇解答xstream转换过程中可能出现问题的作文
  1. package com.iteye.am-human.message.msgDefinition;  
  2.   
  3. import com.thoughtworks.xstream.XStream;  
  4. import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;  
  5. import com.thoughtworks.xstream.io.xml.XppDriver;  
  6.   
  7. /** 
  8.  * 消息结构的抽象基类 
  9.  *  
  10.  * @author Wuyc 
  11.  * 
  12.  */  
  13. public abstract class BaseMsg {  
  14.     public BaseMsg(){}  
  15.       
  16.     public BaseMsg(Head head) {  
  17.         super();  
  18.         this.head = head;  
  19.     }  
  20.   
  21.     public BaseMsg(int commandId){  
  22.         super();  
  23.         this.head = new Head(commandId,getSequence());  
  24.     }  
  25.       
  26.     private Head head;  
  27.       
  28.     public Head getHead() {  
  29.         return head;  
  30.     }  
  31.   
  32.     public void setHead(Head head) {  
  33.         this.head = head;  
  34.     }  
  35.       
  36.     /** 
  37.      * 将当前对象转换成xml数据 
  38.      *  
  39.      * @return xml数据 
  40.      */  
  41.     public String toXml(){  
  42.         XStream xs = new XStream(new XppDriver(new  
  43.                 XmlFriendlyReplacer("_-""_")));  
  44.         xs.autodetectAnnotations(true);  
  45.         return xs.toXML(this);  
  46.     }  
  47.       
  48.     protected static String getSequence(){  
  49.         return "1"+Math.round(Math.random()*10e14);  
  50.     }  
  51. }  


Java代码  一篇解答xstream转换过程中可能出现问题的作文
  1. package com.iteye.am-human.message.msgDefinition;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Date;  
  5. import java.util.List;  
  6.   
  7. import com.thoughtworks.xstream.XStream;  
  8. import com.thoughtworks.xstream.annotations.XStreamAlias;  
  9. import com.thoughtworks.xstream.io.xml.DomDriver;  
  10.   
  11. /** 
  12.  * <xmp> 
  13.  * <?xml version="1.0" encoding="UTF-8" ?> 
  14.  * <root> 
  15.  *  <head> 
  16.  *      <command>5011</command> 
  17.  *      <sequence>编号</sequence> 
  18.  *  </head> 
  19.  *  <body> 
  20.  *      <data> 
  21.  *          <flow_list> 
  22.  *              <flow> 
  23.  *                  <flow_instance_id></flow_instance_id> 
  24.  *                  <flow_status></flow_status> 
  25.  *                  <flow_start_time></flow_start_time> 
  26.  *                  <srv_list> 
  27.  *                      <service> 
  28.  *                          <service_id></service_id> 
  29.  *                          <proc_list> 
  30.  *                              <proc> 
  31.  *                                  <proc_id></proc_id> 
  32.  *                                  <pid></pid> 
  33.  *                                  <vm_id></vm_id> 
  34.  *                                  <handle_count></handle_count> 
  35.  *                                  <handle_speed></handle_speed> 
  36.  *                                  <proc_start_time></proc_start_time> 
  37.  *                              </proc> 
  38.  *                          </proc_list> 
  39.  *                      </service> 
  40.  *                  </srv_list> 
  41.  *              </flow> 
  42.  *          </flow_list> 
  43.  *      </data> 
  44.  *  </body> 
  45.  * </root> 
  46.  * </xmp> 
  47.  *  
  48.  * @author Wuyc 
  49.  * 
  50.  */  
  51. @XStreamAlias("root")  
  52. public class Msg5011Def extends BaseMsg {  
  53.     private static final int COMMAND_ID = 5011;  
  54.       
  55.     private Body body;  
  56.       
  57.     public Msg5011Def(){  
  58.         super(COMMAND_ID);  
  59.         this.body = new Body();  
  60.     }  
  61.       
  62.     public static Msg5011Def fromXml(String xml){  
  63.         XStream xstream = new XStream(new DomDriver());  
  64.         xstream.alias("root",Msg5011Def.class);  
  65.         xstream.alias("flow",Msg5011Def.Body.Data.Flow.class);  
  66.         xstream.alias("service",Msg5011Def.Body.Data.Flow.Service.class);  
  67.         xstream.alias("proc",Msg5011Def.Body.Data.Flow.Service.Process.class);  
  68.         xstream.registerConverter(new DateSingleValueConverter());  
  69.         return (Msg5011Def)xstream.fromXML(xml);  
  70.     }  
  71.       
  72.     public static class Body extends BaseBody{  
  73.         private Data data;  
  74.           
  75.         public Body(){  
  76.             this.data = new Data();  
  77.         }  
  78.           
  79.         public static class Data{  
  80.             private List<Flow> flow_list;  
  81.               
  82.             public Data(){  
  83.                 this.flow_list = new ArrayList<Flow>();  
  84.             }  
  85.               
  86.             @XStreamAlias("flow")  
  87.             public static class Flow{  
  88.                 private int flow_instance_id;  
  89.                 private int flow_status;  
  90.                 private Date flow_start_time;  
  91.                   
  92.                 private List<Service> srv_list;  
  93.                   
  94.                 public Flow(){  
  95.                     this.srv_list = new ArrayList<Service>();  
  96.                 }  
  97.                   
  98.                 @XStreamAlias("service")  
  99.                 public static class Service{  
  100.                     private int service_id;  
  101.                     private List<Process> proc_list;  
  102.                       
  103.                     public Service(){  
  104.                         this.proc_list = new ArrayList<Process>();  
  105.                     }  
  106.                       
  107.                     @XStreamAlias("proc")  
  108.                     public static class Process{  
  109.                         private int proc_id;  
  110.                         private int pid;  
  111.                         private int vm_id;  
  112.                         private int handle_count;       // 当天的处理量  
  113.                         private float handle_speed;     // 处理速度  
  114.                         private Date proc_start_time;   // 进程启动时间  
  115.                         public int getProc_id() {  
  116.                             return proc_id;  
  117.                         }  
  118.                         public int getPid() {  
  119.                             return pid;  
  120.                         }  
  121.                         public int getVm_id() {  
  122.                             return vm_id;  
  123.                         }  
  124.                         public int getHandle_count() {  
  125.                             return handle_count;  
  126.                         }  
  127.                         public float getHandle_speed() {  
  128.                             return handle_speed;  
  129.                         }  
  130.                         public Date getProc_start_time() {  
  131.                             return proc_start_time;  
  132.                         }  
  133.                     }  
  134.                     public int getService_id() {  
  135.                         return service_id;  
  136.                     }  
  137.                     public List<Process> getProc_list() {  
  138.                         return proc_list;  
  139.                     }  
  140.                 }  
  141.   
  142.                 public int getFlow_instance_id() {  
  143.                     return flow_instance_id;  
  144.                 }  
  145.   
  146.                 public int getFlow_status() {  
  147.                     return flow_status;  
  148.                 }  
  149.   
  150.                 public List<Service> getSrv_list() {  
  151.                     return srv_list;  
  152.                 }  
  153.   
  154.                 public Date getFlow_start_time() {  
  155.                     return flow_start_time;  
  156.                 }  
  157.             }  
  158.   
  159.             public List<Flow> getFlow_list() {  
  160.                 return flow_list;  
  161.             }  
  162.         }  
  163.   
  164.         public Data getData() {  
  165.             return data;  
  166.         }  
  167.     }  
  168.   
  169.     public Body getBody() {  
  170.         return body;  
  171.     }  
  172. }  


Java代码  一篇解答xstream转换过程中可能出现问题的作文
  1. package com.iteye.am-human.message.msgDefinition;  
  2.   
  3. import java.util.Date;  
  4. import java.text.ParseException;  
  5. import java.text.SimpleDateFormat;  
  6.   
  7. import com.thoughtworks.xstream.converters.Converter;  
  8. import com.thoughtworks.xstream.converters.MarshallingContext;  
  9. import com.thoughtworks.xstream.converters.UnmarshallingContext;  
  10. import com.thoughtworks.xstream.io.HierarchicalStreamReader;  
  11. import com.thoughtworks.xstream.io.HierarchicalStreamWriter;  
  12.   
  13. /** 
  14.  * Date数据转换器 
  15.  * 默认的format格式为:yyyyMMddHHmmss 
  16.  *  
  17.  * @author Wuyc 
  18.  * 
  19.  */  
  20. public class DateSingleValueConverter implements Converter {  
  21.     private SimpleDateFormat sdf;  
  22.       
  23.     public DateSingleValueConverter(){  
  24.         this.sdf = new SimpleDateFormat("yyyyMMddHHmmss");  
  25.     }  
  26.       
  27.     public DateSingleValueConverter(String formatString){  
  28.         this.sdf = new SimpleDateFormat(formatString);   
  29.     }  
  30.   
  31.       
  32.     public void marshal(Object arg0, HierarchicalStreamWriter writer,  
  33.             MarshallingContext arg2) {  
  34.         Date date = (Date)arg0;  
  35.         writer.setValue(sdf.format(date));  
  36.     }  
  37.   
  38.     public Object unmarshal(HierarchicalStreamReader reader,  
  39.             UnmarshallingContext arg1) {  
  40.         Date date = null;  
  41.         try {  
  42.             String strDate = reader.getValue();  
  43.             if(strDate != null && strDate.length()!=0 && !"".equals(strDate) && !"0".equals(strDate)){  
  44.                 date = sdf.parse(reader.getValue());  
  45.             }  
  46.         } catch (ParseException e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.         return date;  
  50.     }  
  51.   
  52.     public boolean canConvert(Class type) {  
  53.         return Date.class.equals(type);  
  54.     }  
  55.   
  56. }  


其他的xml转换框架: 
castor 
官网: http://www.castor.org/  
通过定义mapping文件,实现xml和javabean的对应。 

JOX 
官网: http://www.wutka.com/jox.html  
sourceforge(官网打不开): http://sourceforge.net/projects/jox/