Hi again,
Finally, I was able to look into this and it works correctly for me! (XmlBeans
2.4.0)
Here's the complete Schema (extrapolated by me based on the info that you
provided):
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://cfnxml"
targetNamespace="http://cfnxml">
<xs:element name="DOC">
<xs:complexType>
<xs:sequence>
<xs:element name="items" type="Children"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Children">
<xs:sequence>
<xs:element name="item" type="Parent" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Parent" abstract="true">
</xs:complexType>
<xs:complexType name="Child1">
<xs:complexContent>
<xs:extension base="Parent">
<xs:sequence>
<xs:element name="text" type="xs:string"/>
</xs:sequence>
<xs:attribute name="attr" type="xs:string"></xs:attribute>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Child2">
<xs:complexContent>
<xs:extension base="Parent"/>
</xs:complexContent>
</xs:complexType>
</xs:schema>
Details can definitely make a difference, so I posted the whole thing. This is
the code that I have written (also based on your post):
DOCDocument.DOC enclosingElement =
DOCDocument.Factory.newInstance().addNewDOC();
// Create and populate a Child1 instance.
Child1 child1 = Child1.Factory.newInstance();
child1.setAttr("attributeValue"); // Sets an attribute
child1.setText("some text"); // Set a subelementt
// Substitute the Child1 instance into the childrenList, which is a
list of
// type Parent. Assume an enclosing type has defined an element
// named children of complex type Children as specified in my original
// posting.
Children childrenList = enclosingElement.addNewItems();
Parent[] parentArray = new Parent[1];
parentArray[0] = child1;
childrenList.setItemArray(parentArray);
System.out.println("Final document: " + enclosingElement.xmlText(new
XmlOptions().setSaveOuter()));
And the output:
Final document: <cfn:DOC xmlns:cfn="http://cfnxml"><items><item
attr="attributeValue" xsi:type="cfn:Child1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><text>some
text</text></item></items></cfn:DOC>
As expected, the xsi:type attribute is rendered. Try starting from this example
and build it into what you need.
Radu
________________________________
From: Radu Preotiuc-Pietro [mailto:[EMAIL PROTECTED]
Sent: Wednesday, October 29, 2008 9:12 PM
To: [email protected]
Subject: RE: Problem with list of derived types
Richard,
Your original design should work, the hack is really ugly. I haven't
had any time so far, but I plan to try your example and see if it works for me
or it's a bug or we're missing something.
Radu
________________________________
From: Richard J Cardone [mailto:[EMAIL PROTECTED]
Sent: Sunday, October 26, 2008 9:48 AM
To: [email protected]
Subject: RE: Problem with list of derived types
Radu,
Thanks for taking a look at this problem. I came up with a
hack that works, but I would prefer to go back to the original design. Here's
a reconstruction of my original code under my original design:
// Create and populate a Child1 instance.
Child1 child1 = Child1.Factory.newInstance();
child1.setAttr("attributeValue"); // Sets an
attribute
child1.setText("some text"); // Set a
subelementt
// Substitute the Child1 instance into the
childrenList, which is a list of
// type Parent. Assume an enclosing type has defined
an element
// named children of complex type Children as specified
in my original
// posting.
Children childrenList =
enclosingElement.addNewChildren();
Parent[] parentArray = new Parent[1];
parentArray[0] = child1;
childrenList.setChildrenArray[parentArray];
Since there was no easy way to directly create a Child1
instance in the childrenList, I independently created a Child1 instance, put it
into a array, and substituted that array for the array in the childrenList. As
I mentioned in my original posting, the above code doesn't work. Since then,
I've come up with a hack that works but isn't very pretty. The hack defines a
choice type that causes a list of heterogeneous types that are all subtypes of
Parent to be generated by Xmlbeans.
<xs:complexType name="ChildrenHack" >
<xs:choice minOccurs="0"
maxOccurs="unbounded">
<xs:element name="child1"
type="Child1" />
<xs:element name="child2" type="Child2"
/>
</xs:choice>
</xs:complexType>
This hack cause Xmlbeans to generate code that I use as
follows:
ChildrenHack childrenHack =
enclosingElement.addNewChildrenHack()
Child1 child1 = childrenHack.addNewChild1();
...
I'm definitely interested in learning the correct way to
populate a list of general types with instances of specific subtypes. Save me
from this hack if you can!
Rich