Hi Pavel

The trouble with mixed content is that text can occur mixed in any order
with element content. Exactly how the user wants to use that differs
from user to user - which is why XmlBeans provides the more general
XmlCursor API that the user can use to do whatever they want (and more
specifically is why there is no general "getText()" method on every
generated bean - as there was no agreement as to which bits of text this
should return).

If pointed at an ELEM token getTextValue() will return you the text from
all of the individual text tokens (both for its own text content and its
children's) concatenated together. This is OK if you are sure that the
element will contain _only_ text or children (you could use the
toFirstChild() API to check whether it has any children). But you're
right, in more complicated cases it's not enough.

You can also navigate individually to each TEXT token (at the correct
depth if that is what you want) e.g. using TokenType.isText() - then
getTextValue() will return you the text for just that token (your use of
getChars() will work too since you are pointing only at TEXT tokens).

But your code looks fine. If you want to separate out the different
parts of text, you could always put each text value in a List instead of
concatenating them together - but whatever suits your application. I'm
not sure what else you need?

Cheers,

Lawrence

> -----Original Message-----
> From: Pavel Krupets [mailto:[EMAIL PROTECTED]
> Sent: Monday, March 27, 2006 12:22 PM
> To: [email protected]
> Subject: RE: Help needed with mixed content...
> 
> 
> > XmlCursor c = xmlo.newCursor();
> > c.toFirstContentToken();
> > String s = c.getTextValue();
> > c.dispose();
> 
> Thanks for your reply. But how can I get next characters sequence? I
> implemented code which tracks number of START/END (depth) and only
takes
> those which have depth=0 but I used code like this (with code you
provided
> I
> don't know how to get text which split into several sequences):
> 
>         int depth = 0;
>         XmlCursor cursor = attr.newCursor();
>         StringBuffer value = new StringBuffer();
> 
>         while (!cursor.toNextToken().isNone()) {
>             if (cursor.currentTokenType() ==
XmlCursor.TokenType.START) {
>                 depth++;
>             } else if (cursor.currentTokenType() ==
> XmlCursor.TokenType.END)
> {
>                 depth--;
>             } else if (depth == 0 && cursor.currentTokenType() ==
> XmlCursor.TokenType.TEXT) {
>                 String str = cursor.getChars().trim();
> 
>                 if (str != null && str.length() > 0) {
>                     value.append(str);
>                 }
>             }
>         }
> 
>         return value.length() == 0 ? null : value.toString();
> 
> 
>   _____
> 
> From: Lawrence Jones [mailto:[EMAIL PROTECTED]
> Sent: Monday, March 27, 2006 10:41 PM
> To: [email protected]
> Subject: RE: Help needed with mixed content...
> 
> Hi Pavel
> 
> The content model you are referring to is called a "mixed" content
model.
> You specify this in your schema using the mixed="true" attribute:
either
> 
> <complexType : mixed="true" >
>  :
> </complexType>
> 
> Or
> 
> <complexType>
>   <complexContent mixed="true">
>     :
>   </complexContent>
> </complexType>
> 
> See the schema spec
> (http://www.w3.org/TR/xmlschema-1/#Complex_Type_Definitions) for
details.
> 
> If you use XmlBeans on a schema like that then we do not generate a
> gettter/setter for the text which may (or may not) be inside the
element
> in
> the instance doc. Instead you use the XmlCursor APIs. Once you have an
> XmlObject (xmlo) do something like this:
> 
> XmlCursor c = xmlo.newCursor();
> c.toFirstContentToken();
> String s = c.getTextValue();
> c.dispose();
> 
> The following links may also be useful to you:
> 
> http://xmlbeans.apache.org/documentation/tutorial_getstarted.html (the
> part
> labelled "Getting Started with the XML Cursor" near the bottom)
>
http://xmlbeans.apache.org/docs/2.0.0/guide/conNavigatingXMLwithCursors.
ht
> ml
> http://dev2dev.bea.com/pub/a/2003/11/traut.html
> 
> Cheers,
> 
> Lawrence
> 
>   _____
> 
> From: Pavel Krupets [mailto:[EMAIL PROTECTED]
> Sent: Sunday, March 26, 2006 2:12 AM
> To: [email protected]
> Subject: Help needed with mixed content...
> 
> Hello,
> 
> I need to implement something like this:
> 
> <attribute name=":" class=":">
>   <attribute name=":">value</attribute>
>   <attribute name=":">value</attribute>
> /attribute>
> 
> and
> 
> <attribute name=":">value</attribute>
> 
> What I mean is that attribute element can contain sub elements OR
text.
> What
> do I need to use to implement this? Note that I need to read document
(it
> will be typed manually by user).
> 
> With regards,
> Pavel Krupets
>
_______________________________________________________________________
> 
> 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]

_______________________________________________________________________
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