I found a way in the API to circumvent the Java reflection code i posted
earlier.
SchemaTypeLoader.newInstance(SchemaType, XmlOptions) creates the empty typed
XmlObject
hope this helps others.
regards
kapil
Kapil Anand wrote:
>
> hi
> I am running into problem while trying to extract an xs:any as a typed
> XMLObject.
> I am writing code that is not aware of the actual concrete types but
> responsible for inserting and extracting XmlObjects as xs:any (and
> dynamically converting to concrete type while extracting)
>
> I have following:
> <xs:complexType name="MyType">
> ...
> </xs:complexType>
>
> <xs:element name="MyElement" type="MyType"/>
>
> My code receives MyElementDocument and it inserts it as xs:any in a parent
> element using XmlCursor and this works fine. But when I extract the xs:any
> using parent.selectPath("./*") I get an XML fragment which is of class
> XmlAnyTypeImpl with the contents of MyType and the XmlCursor's name same
> as that of the MyElementDocument's QName. (This makes sense since document
> can only be an independent instance and not a child)
>
> Subsequent code to do changeType (to dynamically convert to concrete
> Element) on this untyped XmlObject fails to work. Here is my code to do
> dynamic conversion:
>
> SchemaTypeLoader typeLoader = XmlBeans.getContextTypeLoader();
> XmlObject[] anyChildren = parent.selectPath("./*");
> XmlObject anyChild = anyChildren[0];
> XmlCursor cursor = anyChild.newCursor();
> QName qName = cursor.getName();
> // The qName here is the QName of MyElementDocument
> SchemaType schType = typeLoader.findDocumentType(qName);
> XmlObject typedObj = anyChild.changeType(schType);
>
> if (typedObj == anyChild)
> {
> System.out.println("Could not change type");
> }
> else
> {
> // This object can now be treated as MyElementDocument
> }
>
>
> Am I doing something wrong here? Is there some other way to achieve this
> without requiring to know the actual concrete XmlObject and its Factory?
>
> thanks for the help.
> regards
> kapil
>
--
View this message in context:
http://www.nabble.com/Adding-Removing-xml-documents-as-xs%3Aany-tp24062950p24081579.html
Sent from the Xml Beans - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]