users@jaxb.java.net

Re: JAXB and xsd:any values

From: jmdev <jhmiller001_at_yahoo.com>
Date: Wed, 29 Apr 2009 13:05:15 -0700 (PDT)

Thank you Wolfgang, I didn't realize that xsd:any mandated an element. I
thought it could be "anything" including an xsd:string.


Here we have the element ns2:Message and its type:

      <xsd:element name="Message">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:any namespace="##any" processContents="lax"
                     minOccurs="1" maxOccurs="1"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>

This tells us that some compliant XML content must be s.th. like
   <Message>
     <whatever>
        and here you may really do what you like
     </whatever>
   </Message>

but you cannot have, say
   <Message>Hello world</Message>
as this would contradict the Schema type.

What tag you use in place of <whatever> is entirely up to you, and you
may set this
arbitrarily, at runtime, for each individual Message element.

Here is a simple method:

   <T> JAXBElement<T> wrap( String ns, String tag, T o ){
        QName qtag = new QName( ns, tag );
        Class<?> clazz = o.getClass();
        @SuppressWarnings( "unchecked" )
        JAXBElement<T> jbe = new JAXBElement( qtag, clazz, o );
        return jbe;
    }

to be used like this:
        JAXBElement<String> jbe =
           wrap( "bar.b.cue", "whatever", "Hello World" );
        msg.setAny( jbe );

to give you


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Document xmlns="foo">
    <Message>
        <ns2:whatever xmlns:ns2="bar.b.cue">Hello World</ns2:whatever>
    </Message>
</Document>


-W



-- 
View this message in context: http://www.nabble.com/JAXB-and-xsd%3Aany-values-tp22860952p23301707.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.