users@jaxb.java.net

Re: How do I get the CDATA data ?

From: J. Medina <jmedina_at_on.com>
Date: Thu, 03 Apr 2003 14:59:39 +0200

Thanks Robert,
 Certainly your suggestion helps to code it once, but still the client code
need to be aware of the meaning of this content list.





Robert Lowe <rmlowe_at_rmlowe.com>@JAVA.SUN.COM> on 04/03/2003 02:25:37 PM

Please respond to Discussion list for the Java Architecture for XML Binding
       <JAXB-INTEREST_at_JAVA.SUN.COM>

Sent by: Discussion list for the Java Architecture for XML Binding
       <JAXB-INTEREST_at_JAVA.SUN.COM>


To: JAXB-INTEREST_at_JAVA.SUN.COM
cc:

Subject: Re: How do I get the CDATA data ?


Could you move the code into a couple of utility methods as below?

public class JAXBUtils {
    public static String getText(List content) {
        String res = null;
        Iterator iter = content.iterator();
        while( iter.hasNext() ) {
            Object data = iter.next();
            if( data instanceof String) {
                res = (String) data;
            }
        }
        return res;
    }

    public static void setText(List content, String text) {
        Iterator iter = content.iterator();
        while( iter.hasNext() ) {
            Object data = iter.next();
            if( data instanceof String) {
                iter.remove();
            }
        }
        content.add( text );
    }
}

Then you could just write:

String text = JAXBUtils.getText(myElement.getContent());
JAXBUtils.setText(myElement.getContent(), text);


----- Original Message -----
From: "J. Medina" <jmedina_at_on.com>
To: <JAXB-INTEREST_at_JAVA.SUN.COM>
Sent: Thursday, April 03, 2003 2:50 PM
Subject: Re: How do I get the CDATA data ?


> I have xml schema that allows mixed content (CDATA or any free text
> between the start and end tag).
> The generated JAXB classes do not provide a method to set this part of
the
> data.
>
> <myElement myAttribute= "myAttributeValue">
> some text here or<![CDATA[some free text here that can even
> include <some> <text> similar to <xml>]]>
> </myElement>
>
>
>
> The generated classes by JAXB would provide a setMyAttribute
> ()/getMyAttribute() method and will provide a getContent() method.
>
> The getContent() method provides a java.util.List , one of the elements
of
> the list will be an instance of java.lang.String. The string contains the
> text:
>
> "some text here orsome free text here that can even include <some>
> <text> similar to <xml>"
>
> In other words, all the Text and CDATASection nodes from a DOM tree are
> mapped to a single string.
>
> Currently JAXB generates the interface:
>
> public interface MyElement {
>
> void setMyAttribute(String text);
> String getMyAttribute()
> java.util.List getContents()
>
> }
>
> But since all the mixed content is mapped to a single string , why not
> provide soemthing like:
>
> public interface MyElement {
>
> void setMyAttribute(String text);
> String getMyAttribute()
>
> String getText();
> void setText(String text);
> java.util.List getContents()
>
> }
>
>
> The implementation would be something like:
>
> public String getText() {
> String res = null;
> Iterator iter = getContent().iterator();
> while( iter.hasNext() ) {
> Object data = iter.next();
> if( data instanceof String) {
> res = (String) data;
> }
> }
> return res;
> }
>
> public void setText(String text) {
> Iterator iter = getContent().iterator();
> while( iter.hasNext() ) {
> Object data = iter.next();
> if( data instanceof String) {
> iter.remove();
> }
> }
> getContent().add( text );
> }
>
>
> My schema has several elements that allows mixed content, and I find
myself
> repeating the code above in a new class for every element that allows
mixed
> content.
> It would be useful to have the setText() / getText() methods generated by
> JAXB.
>
> But, if not possible, it would be enough if the generated interfaces that
> have the getContent() method extend a marker interface....In that way, I
> can may a single class with teh methods above and not repeat them for
every
> single element with mixed content.
>
> -Jorge
>
>
>
>
>
>
>
> Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>@JAVA.SUN.COM> on 04/02/2003
> 05:22:06 PM
>
> Please respond to JAXB-INTEREST_at_JAVA.SUN.COM
>
> Sent by: Discussion list for the Java Architecture for XML Binding
> <JAXB-INTEREST_at_JAVA.SUN.COM>
>
>
> To: JAXB-INTEREST_at_JAVA.SUN.COM
> cc:
>
> Subject: Re: How do I get the CDATA data ?
>
>
> > Since all text and CDATA nodes are being put together into a single
> > element of the list returned by getContent() , Why not provide a
setText
> ()
> > / getText() method on those classed that handle mixed content ?
> >
> > Or at least, provide a marker interface MixedContent for those classes
> with
> > the getContent() method. This would help to implement a generic class
to
> > set, get the text on those elements.
>
> I'm afraid I don't understand what you mean.
>
> regards,
> --
> Kohsuke KAWAGUCHI 408-276-7063 (x17063)
> Sun Microsystems kohsuke.kawaguchi_at_sun.com