You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xmlbeans.apache.org by puneet sharma <pu...@yahoo.com> on 2005/03/11 12:12:14 UTC

getObject() always returns XmlAnyTypeImpl

Hi All,

I am facing problems while getting the child element
object from the XmlCursor. Prolem is getObject()
method of XmlCursor always returns XmlAnyTypeImpl type
of object.

I am using XMLBeans v 1.0.3 Development Kit

I have defined two elements, my schema definition
looks like. 


<xsd:complexType name="ParentType">
   <xsd:sequence>
     <xsd:any namespace="##any" minOccurs="0"
maxOccurs="unbounded"/>
   </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="ChildType">
	<xsd:sequence>
		<xsd:element name="subChild" type="subChildType" /> 
	  </xsd:sequence>
</xsd:complexType>

(Notice 'ParentType' is having 'any' as child.)

   There are no direct methods available to get/set
the any type child values in XmlBeans.
   So I wrote two utility methods getChild/setChild in
my program. 

   The calling code looks like.
	   ParentTypeImpl body =
ParentType.Factory.newInstance();
	   setTransaction(body);
	   getTransaction(body);

   When I run this code it always prints following
lines and throws ClassCastException.
	   
'ChildClass=org.apache.xmlbeans.impl.values.XmlAnyTypeImpl'
	  
'TypeCastedClassName=org.apache.xmlbeans.impl.values.XmlAnyTypeImpl'

   I am not able to understand why the getObject()
method returns a XmlAnyType object
   It should return ChildType object.

    public static void setChild(ParentType parent) {
        ChildTypeImpl child = (ChildTypeImpl)
ChildType.Factory.newInstance();        
        XmlCursor parentCursor = parent.newCursor();
        parentCursor.toFirstContentToken();
       
parentCursor.insertElement(ChildType.type.getName());
        parentCursor.dispose();
    }

    public static ChildType getChild(ParentType
parent) {
        XmlCursor parentCursor = parent.newCursor();
        boolean childPresent =
parentCursor.toChild(ChildType.type.getName());
        try{
	        if (childPresent) {
	            XmlObject childObject =
parentCursor.getObject();
	           
System.out.println("ChildClass="+child.getClass().getName());

		    XmlObject child =
object.changeType(ChildType.type);
		   
System.out.println("TypeCastedClassName="+child.getClass().getName());
	            return (ChildType) child;
	        }
	        else {
	            System.out.println("Child not present");
	        }
        }
        finally{
            parentCursor.dispose() ;    
        }
        return null;
    }


Thanks in advance,
Puneet



		
__________________________________ 
Do you Yahoo!? 
Make Yahoo! your home page 
http://www.yahoo.com/r/hs

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: dev-help@xmlbeans.apache.org