You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Ca...@vrtx.com on 2003/10/01 18:37:23 UTC

problems with inheritance while saving XML

Hi all,

Sorry if this is a dupe.  I am finding that when an object is written out 
to an XML file, the wrong class name is used.   Specifically, I have a 
class MSIDTO that inherits from DTO. Another class, DTOCollection, can 
contain DTOs.   I want to be able to add MSIDTOs to my DTOCollection, save 
the collection to disk, read it back in again, and get back my original 
MSIDTOs. But when I do this, they are written (and hence read back) as 
DTOs. 

Here is my schema: 

<xs:element name="DTOCollection">
     <xs:complexType> 
          <xs:sequence>
                 <xs:element name="DTO" type="dto:DTO" minOccurs="0" 
maxOccurs="unbounded"/> 
          </xs:sequence>
     </xs:complexType> 
</xs:element> 

<xs:complexType name="DTO"/> 

<xs:complexType name="MSIDTO"> 
   <xs:complexContent> 
      <xs:extension base="dto:DTO">
              <xs:sequence> 
                     <xs:element name="id" type="xs:long"/> 
              </xs:sequence> 
      </xs:extension> 
    </xs:complexContent> 
</xs:complexType> 

I write out this schema with the following code: 


XmlDTOBean[] dtoArray = new XmlDTOBean [towrite.size()]; .... //populate 
array with MSIDTOs ... XmlDTOCollectionDocumentBean.DTOCollection collect 
= dtos.addNewDTOCollection(); collect.setDTOArray(dtoArray); //save array 
dtos.save(new File ("c:/temp/problem.txt")); 

I would expect that the resulting XML file should contain MSIDTOs, since 
the objects in the array are actually MSIDTOs. However, the elements are 
actually DTOs. (They nonetheless have an ID, even though only MSIDTO has 
this attribute) 

<DTO> <id>2</id> </DTO> <DTO> <id>3</id> </DTO> 


Apparently, they are DTOs because dtoArray is declared to be an array of 
XMLDTOBean. 

How can I force the file to contain the actual object type, not the type 
of the containing array? (I can't simply change the type of the containing 
array, because DTO will later have many subclasses.) 

Thanks in advance, 
Carl