> Is there a way to create a document type XmlObject which has the root > element "elem1" and it shows up in the xmlText() along with the previously > selected contents?
Kapil, it may help if you read http://xmlbeans.apache.org/docs/2.0.0/guide/conJavaTypesGeneratedFromUserDerived.html. First of all, it bears repeating that an XmlObject represents an instance of an XML Schema type, i.e., it represents the *contents* of an xml element or attribute, not the element or attribute itself. Secondly, XMLBeans generates a Document interface for global elements in a schema, which could potentially be the root element of some xml document. There is no Document interface generated for elements that are not global, so in general, you cannot expect there to be one for elem1 or elem2 in your example. The Document represents an xml document containing the global element, *not* the element. Working with XmlCursor, on the other hand, is a different paradigm where you're working at a low level, directly navigating the xml itself. So, you will see things such as the QName of the elements and so on that you encounter. Hope that helps, Wing Yew -----Original Message----- From: Kapil Anand [mailto:[email protected]] Sent: Thursday, March 12, 2009 12:59 PM To: [email protected] Subject: RE: Navigating Untyped XmlObject with XmlCursor Thanks Cezar. I am now aware of the things you pointed out. What I am looking for is some way to get XmlObject that represents the element and not the type. This might map to document type XmlObject, am I correct? So the the problem is reduced to: Once I have selected the XmlObject representing the contents of a child element (by calling cursor.getObject()), how do I create an untyped document XmlObject based on the knowledge of the cursor name. To elaborate on what I am trying to do, consider the XML whose root element is rootElem and it has two children elem1 and elem2: rootElem -> elem1 -> child1 -> child2 -> elem2 After using cursor.getObject() for firstChild, I get an XmlObject which has xmlText of the child-contents of "elem1" and the cursor name is "elem1". If I pass this object to another method as XmlObject it will need to recreate its cursor to determine its name. Is there a way to create a document type XmlObject which has the root element "elem1" and it shows up in the xmlText() along with the previously selected contents? If I call another method with this document-type XmlObject, the code in this different method will still need to create a cursor to determine its qname, but now I can at least string compare the contents with another XMLObject with was parsed from a different source as a document-type. regards kapil Cezar Andrei-3 wrote: > > Kapil, > > You don't need to create a new cursor, if the item your cursor points to > has a name (i.e. element/attribute/pi) just call getName() on it. Check > out the javadoc of XmlCursor > http://xmlbeans.apache.org/docs/2.4.0/reference/org/apache/xmlbeans/XmlCursor.html > and XmlObject > http://xmlbeans.apache.org/docs/2.4.0/reference/org/apache/xmlbeans/XmlObject.html. > > An XmlObject represents the xml instance of the schema type that is > assigned to, that's why you see xml-fragment. Take a look at the save > XmlOptions, saveOutter might be what you're looking for: > http://xmlbeans.apache.org/docs/2.4.0/reference/org/apache/xmlbeans/XmlOptions.html#setSaveOuter(). > > Cezar > >> -----Original Message----- >> From: Kapil Anand [mailto:[email protected]] >> Sent: Wednesday, March 11, 2009 10:20 PM >> To: [email protected] >> Subject: Re: Navigating Untyped XmlObject with XmlCursor >> >> >> OK I figured it out. >> It was my lack of understanding of how cursor.getObject() works. >> There must be some good reason behind this design but it sure is tricky. >> >> xmlObj = cursor.getObject() returns the typed contents of the current >> start/startdoc cursor position and it excludes the container element >> itself >> and so it does not show up in xmlObj.xmlText(). But it does store the >> QName >> of the parent element somewhere as it can be accessed again by calling >> xmlObj.newCursor.getName(). It would be good though if this could be >> accessible without creating the new cursor. >> >> Now I am wondering what would be the way to create the container >> XMLObject >> from this one, so the xmlText shows the container element name as well. >> Tried two things without success: >> 1) XMLObject.Factory.parse(xmlObj.newXMLStreamReader()). The xmlText() >> remains the same, perhaps the new one might have lost the information >> about >> the cursor name that represented the container element. >> 2) newObj = XMLObject.Factory.newInstance() >> newObj.set(xmlObj); >> This one threw exception: >> org.apache.xmlbeans.impl.values.XmlValueDisconnectedException >> >> I would appreciate any help or ideas. >> >> regards >> kapil >> >> Kapil Anand wrote: >> > >> > Thanks for replying Jacob. >> > I tried cursor.child(1) but it returns false. (child index 1 anyway >> > represents the second child of the current cursor position and i need >> to >> > select the first child). This is what i am inferring from the behavior >> of >> > the api: >> > >> > When i start with the XmlObject that represents the untyped document, >> the >> > first child under it is an xml-fragment containing the contents of the >> > root element. But this does not make sense. It might make perfect sense >> > perhaps if I had started with a schema and declared things like this: >> > >> > <xs:complexType name="ElemT"> >> > ... >> > </xs:complexType> >> > <xs:element name="RootElem" type="ElemT"/> >> > >> > But I have doubts about this even for typed XmlObject, because it might >> be >> > right if i am trying to get to the next token, but next child should >> > always be a concrete node as per my understanding. >> > >> > This assumption/inference is proved wrong when i try to move to the >> first >> > child of this xml-fragment, it actually takes me to the subsequent >> child, >> > so child navigation is correct the contents of the intermediate child >> are >> > incorrect. >> > >> > I will try to explain in problem with the tree. >> > RootElement >> > -> Level1.1 >> > -> Level2-1.1 >> > -> Level1.2 >> > -> Level2-1.2 >> > -> Level1.3 >> > -> Level2-1.3 >> > First cursor.toChild(0) should return Level1.1 but instead it returns >> all >> > the children of Root inside an xml-fragment. Dont know if this is >> expected >> > behavior but it does not match the behavior of the next call to >> > cursor.toChild(0) since it correctly returns Level2-1.1. So although >> the >> > first time the cursor was pointing to Level1.1, it somehow returned >> wrong >> > XML fragment in cursor.getObject(); >> > >> > When i use an XML with single child for each parent it works fine (no >> > siblings at any level). It returns Level1.1 in the first call and >> > Level2-1.1 in the next call, so on and so forth. >> > >> > regards >> > kapil >> > >> >> -- >> View this message in context: http://www.nabble.com/Navigating-Untyped- >> XmlObject-with-XmlCursor-tp22463857p22469181.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] >> > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > > > -- View this message in context: http://www.nabble.com/Navigating-Untyped-XmlObject-with-XmlCursor-tp22463857p22484248.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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

