users@jaxb.java.net

Re: How do I get the CDATA data ?

From: J. Medina <jmedina_at_on.com>
Date: Thu, 03 Apr 2003 08:50:21 +0200

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