Without going into too much details, in XmlBeans you can process
well-formed documents as well as document fragments. In both cases the
type of the document (the one that you pass into the parse() method)
reflects the content of your document/document fragment.

If your XML looks like <block><something>abc</something></block> then
the type of it will have to contain one <block> element
If your XML looks like
<xml-fragment><something>abc</something></xml-fragment> then
<xml-fragment> by convention gets stripped off and so your type would
have to contain one <something> element. This is why it works with
<xml-fragment>, because <xml-fragment> is special and says "ignore the
root and go one level deep".

So you have two alternatives:
1. Use a type that does have <block> as its content. If <block> is a
global element then XMLBeans generates the BlockDocument.type for this
purpose. Use it in your parse method and then call
BlockDocument.getBlock() to get to the content of your "block" element.
Notice that this implies you know what element will be returned.
2. Do the "stripping off the root and go one level deep" yourself using
a piece of code like:

XmlObject obj = XmlBeans.getContextTypeLoader().parse(n, XmlObject.type,
null);
XmlCursor c = obj.newCursor();
c.toFirstContentToken();
obj = c.getObject();
Block block = (Block) obj.changeType(Block.type);

Radu

-----Original Message-----
From: Ewout Graswinckel [mailto:[EMAIL PROTECTED] 
Sent: Friday, September 29, 2006 2:12 AM
To: user@xmlbeans.apache.org
Subject: Instantiating a section of an xml document

Hi,

I'd like to know if it's possible to instantiate only a single node from
a document. I'm using an xml database to return a node from some
document using xquery/xpath. Once I have that node I'd like to use
xmlbeans to easily access it.

So far I've come up with this:

<doc>
......
<block><something>abc</something></block>
....
</doc>

Class 'Block' represents the block element in the xml.

Node n =
getNodeFromDatabaseUsingXpath("doc('.')/descendant-or-self::block[1]");
Block block = (Block) XmlBeans.getContextTypeLoader().parse(n,
Block.type, null); assertEquals("abc", block.getSomething()); //
block.getSomething() == null

This doesn't give any exceptions, but calling block.getSomething()
simply returns null.

I can get it to work if I let my query return an <xml-fragment> element
instead of <block>, but this is not desired in my case. Is there a
better way to do this?

Thanks,
Ewout


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

_______________________________________________________________________
Notice:  This email message, together with any attachments, may contain
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
entities,  that may be confidential,  proprietary,  copyrighted  and/or
legally privileged, and is intended solely for the use of the individual
or entity named in this message. If you are not the intended recipient,
and have received this message in error, please immediately return this
by email and then delete it.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to