Java XML序列化不会传播到子对象

时间:2011-12-22 19:20:16

标签: java xml serialization

我有这些课程:

public class PartialScanDTO implements Serializable {

    String invoiceNumber;
    String seal;
    Integer truckNumber;
    List<ScanDetailDTO> scanDetails;

... // getters and setters

} 

public class ScanDetailDTO implements Serializable {

    String returnableType;
    String description;
    Integer expectedQuantity;
    List<ReturnableDTO> returnablesList;
    Boolean notInDBError;

.. // getters and setters
}

public class ReturnableDTO implements Serializable {

    String code;
    String typeCode;
    String description;
    Boolean isEditable;
    Boolean codeFormatError;
    .. // getters and setters
}

我正在使用以下代码序列化PartialScanDTO

public static void write(PartialScanDTO ps, String filename) throws Exception {
    XMLEncoder encoder =
            new XMLEncoder(
            new BufferedOutputStream(
            new FileOutputStream(filename)));
    encoder.writeObject(ps);
    encoder.close();
}

不会抛出任何错误或异常,但生成的文件不包含ScanDetailDTOReturnableDTO的XML。这是生成的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.6.0_26" class="java.beans.XMLDecoder"> 
 <object class="dto.PartialScanDTO"> 
  <void property="invoiceNumber"> 
   <string>55rrg</string> 
  </void> 
  <void property="seal"> 
   <string>123123 / 13213</string> 
  </void> 
  <void property="truckNumber"> 
   <int>666</int> 
  </void> 
 </object> 
</java> 

正如您所看到的,我的所有类都实现了Serializable,并且所有属性都是可序列化的,但生成的XML不完整。

关于如何获取完整XML的任何想法?

0 个答案:

没有答案