users@jaxb.java.net

Re: XML content within an element

From: Joe Fialli <joseph.fialli_at_sun.com>
Date: Thu, 20 Feb 2003 19:04:08 -0500

Marcus Walls wrote:
> Kohsuke,
>
> Thanks for the response.
>
>
>>Assuming that the "XXXX" part is defined as an wildcard, I think JAXB
>>will do the right thing by default. You'll get something like:
>>
>> public interface Event {
>> String getType();
>> String getVersion();
>> javax.xml.bind.Element getContent();
>> }
>
>
> I looked up what you meant by wildcard (I really don't know any schema)
> and I now have schema that looks like the following:
>
> <xsd:element name="event">
> <xsd:complexType>
> <xsd:sequence>
> <xsd:any/>
> </xsd:sequence>
> <xsd:attribute name="type" type="xsd:string"/>
> <xsd:attribute name="version" type="xsd:string"/>
> </xsd:complexType>
> </xsd:element>
>
> which produces the interface as above. However, when I try to
> unmarshall the following document:
>
> <event type="type" version="version">
> <hello>goodbye</hello>
> </event>
>
> I get a NullPointerException - stack-trace included below.
> This is presumably either a bug or I'm doing something silly.

At this time, JAXB only handles xs:any @processContents="strict".
This means that the "wildcarded" element does need to be
a global element that has been submitted to the JAXB binding
compiler. In the future, we anticipate mapping arbitrary
wildcard content (<any @processContents="skip"/>) to a
DOM node, since it is impossible to bind an unconstrained
XML content to a statically schema-derived Java representation.
DOM and SAX are much better equiped to handle processing of
dynamic, non-schema constrained content. Read Chapter E.2 in
the JAXB specification for details on how wildcard is
handled in the current version.

So for your example, you must have a global element defined,
either in the current schema or in another schema that was
submitted to the jaxb binding compiler at the same time that
defines the content of the wildcard.


>
> Assuming this problem is fixable, and I end up with a
> javax.xml.bind.Element, you mentioned that I should "cast it to
> another generated interface". I'm not sure how this works!
>
You need to know what elements could be in the wildcard context
and check the instanceof the returned element. It is application
specific on what element content you might expect to
be in the any.

-Joe Fialli, Sun Microsystems

> i.e. How do I get JAXB to unmarshal the "<hello>goodbye</hello>"?
> I guessed that I'd need to create a JAXBContext for the package
> in which the "<hello>goodbye</hello>" objects reside, and then
> pass the javax.xml.bind.Element to the unmarshaller, but there
> doesn't appear to be the methods for doing this?
>
> I guess I'm not really sure what the javax.xml.bind.Element is
> really representing, and I don't see how JAXB can manipulate
> any of the objects representing the <hello> element, because
> it wouldn't know which package the objects resided in?
>
> In short, I'm confused!
>
> Regards,
>
> Marcus
>
>
>
> -- stack-trace mentioned above --
>
> java.lang.NullPointerException
> at com.sun.xml.bind.validator.MSVValidator.childAsElements(MSVValidator.java:249)
> at com.aspective.common.test.event.xml.impl.EventTypeImpl.serializeElements(EventTypeImpl.java:55)
> at com.aspective.common.test.event.xml.impl.EventImpl.serializeElements(EventImpl.java:41)
> at com.sun.xml.bind.validator.MSVValidator._validate(MSVValidator.java:88)
> at com.sun.xml.bind.validator.MSVValidator.validate(MSVValidator.java:69)
> at com.sun.xml.bind.validator.ValidationContext.validate(ValidationContext.java:78)
> at com.sun.xml.bind.validator.ValidatorImpl.validate(ValidatorImpl.java:99)
> at com.sun.xml.bind.validator.ValidatorImpl.validateRoot(ValidatorImpl.java:80)


--