hi
I got this working, but this code might have some scope for improvement in
case there is a better way to do this. I would appreciate if any expert
could comment on this and suggest scope for improvement.

Here is the code:
(Assuming a separate code had inserted a typed document XmlObject as xsd:any
inside the parent, the following code re-creates the XML document from this
xs:any without knowing its actual type)

SchemaTypeLoader typeLoader = XmlBeans.getContextTypeLoader();

XmlObject[] anyChildren = parent.selectPath("./*");

XmlObject anyChild = anyChildren[0];

XmlCursor anyContentCursor = anyChild.newCursor();

// The cursor has the QName of the original Doc but has contents of the doc 
QName qName = anyContentCursor.getName();

// Find out the XSD type from typeLoader (see previous post)
SchemaType schType = typeLoader.findDocumentType(qName);

// Find XmlObject typed interface class
Class typeClass = schType.getJavaClass();

// Factory class is an inner class in this interface
String factoryClassName = typeClass.getName() + "$Factory";

// create Class object for the Factory
Class factoryClass = Class.forName(factoryClassName);

// get handle to newInstance Method via reflection
Method newInstanceMethod = factoryClass.getMethod("newInstance", null);

// Invoke newInstance vis reflection
XmlObject typedDocObj = (XmlObject) newInstanceMethod.invoke(null, null);

// Create Cursor of this empty doc object and move the cursor to content
token                   
XmlCursor typedDocObjCursor = typedDocObj.newCursor();
typedDocObjCursor.toFirstContentToken();

// move the contents of the anyContentCursor into the empty doc cursor
anyContentCursor.moveXml(typedDocObjCursor);

// typedDocObj  now has contents exactly same as before its insertion into
the parent as xsd:any

thanks
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-tp24062950p24080163.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]

Reply via email to