On 7/11/06, Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_sun.com> wrote:
> Kostis Anagnostopoulos wrote:
> > Hi to all,
> >
> > When i try to marshal a schema type-derived java object containing
> > only attributes then i get an empty string, although its attributes
> > have values.
>
> You mean, you get:
>
> <executionInfo transformerProgramVersion="..."
> configFile="..."
> configFileVersion="..."></executionInfo>
>
> as the output? Or just <executionInfo></executionInfo> (or
> <executionInfo />) without any attribute values?
>
> > See for instance the "executionInfoType" at the end of the mail.
>
> I saw the schema definition but not the instance.
>
> >
> > When i modified my schema, and added on nested element, and
> > additionally set it indeed to something, then i got all content
> > (attributes plus nested element).
> >
> > Alternatively, i worked around that problem by adding one top-level
> > element of "executionInfo" type, and used that element for marshaling.
>
> Ah, that's why. In JAXB 1.x, you can't just marshal an object that
> represents a type. You always need to marshal an instance that maps to
> an element.
>
> I believe there's a general purpose element wrapper code somewhere in
> the archive, which is useful for cases like this.
>
> Maybe this?
> https://jaxb.dev.java.net/servlets/ReadMsg?listName=users&msgNo=997
T class contained in that thread, does not run with current JAXB-1.x,
requires the deprecated XmlSerializable.
I have updated the ElementWrapper and it now uses the generated
runtime classes, which means you have to adapt it for each project.
Here it is:
--------------------------
package gr.forthnet.frepor;
import org.your_xmltypes_packages.impl.runtime.XMLSerializable;
import org.your_xmltypes_packages.impl.runtime.XMLSerializer;
/**
* Use it to wrap ComplexTypes (or even other Elements) inside a new element
* (neccessary since JAXB-1.x cannot marshall complexTypes correctly).
*/
public class ElementWrapper implements XMLSerializable {
private XMLSerializable wrappedElement;
private String namespace;
private String elementName;
public ElementWrapper(String namespace, String elementName,
XMLSerializable objectToWrap) {
this.wrappedElement = objectToWrap;
this.namespace = namespace;
this.elementName = elementName;
}
public void serializeBody(XMLSerializer context)
throws org.xml.sax.SAXException {
context.startElement(namespace, elementName);
wrappedElement.serializeURIs(context);
context.endNamespaceDecls();
wrappedElement.serializeAttributes(context);
context.endAttributes();
wrappedElement.serializeBody(context);
context.endElement();
}
public void serializeAttributes(XMLSerializer context) {
}
public void serializeURIs(XMLSerializer context) {
}
}
--------------------------
>
> > I don't know whether this is considered "normal" (I've found some
> > similar old threads), but either way i believe that 2 actions has to
> > be taken:
> > 1. a notification should be included into the "Limitations" section
> > of the Release Notes,
> > 2. the marshaller should throw an Exception when marshaling types
> > into empty strings.
> >
> > Should i file an issue ?
>
> I agree with you, but I'm afraid we have very limited resources for
> fixing problems in JAXB RI 1.0.x line of code.
>
> If you can write a patch to fix them, I'd be very happy to apply it.
>
> --
> Kohsuke Kawaguchi
> Sun Microsystems kohsuke.kawaguchi_at_sun.com
>
>
>