users@jaxb.java.net

Re: JAXB and xsd:any values

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Wed, 29 Apr 2009 17:13:31 +0200

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 <b>may</b> really do what <i>you</i> 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


On Wed, Apr 29, 2009 at 3:00 PM, jmdev <jhmiller001_at_yahoo.com> wrote:

>
> Ideally, I would like to override the xs:any field to be a xs:string so I
> would end up with a method in Message like setContent(String content). I
> am
> perfectly fine if JAXB escapes any and all XML I may set content to be.
> Since I am simply forwarding messages which I do not have schema for I do
> not have the ability to generate the JAXB bindings ahead of time.
>
> I *think* my question really boils down to whether I can treat the xs:any
> which is defined in the oasis spec as a String in JAXB. My preference
> would
> be not to muck with the OASIS spec or the generated bindings, but to either
> extend the binding classes using Java code or to leverage an external xjc
> binding. Is this possible?
>
> To illustrate with an actual message, I want JAXB to handle the
> WS-Notification pieces, but to effectively treat the Message section as a
> xs:string
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <ns2:Notify xmlns="http://www.w3.org/2005/08/addressing"
> xmlns:ns2="http://docs.oasis-open.org/wsn/b-2"
> xmlns:ns3="http://docs.oasis-open.org/wsrf/bf-2"
> xmlns:ns4="http://docs.oasis-open.org/wsn/t-1">
> <ns2:NotificationMessage>
> <ns2:Topic
> Dialect="http://docs.oasis-open.org/wsn/t-1/TopicExpression/Simple
> ">myTopic</ns2:Topic>
> <ns2:Message>
>
> <!-- INSERT MY STRING HERE (May or may not be XML, but want it to be
> escaped) -->
> hello world
>
> </ns2:Message>
> </ns2:NotificationMessage>
> </ns2:Notify>
>
>
> Wolfgang Laun-2 wrote:
> >
> > In the creation of the JAXBElement, the constructor for QName ought to
> > have
> > a second parameter (for localPart) that is not the empty string; this
> will
> > define the XML tag for your string.
> >
> > If this isn't what you want, please show an example for the XML content
> > you
> > want to have in place of the xs:any.
> >
> > -W
> >
> > On Tue, Apr 28, 2009 at 10:38 PM, jmdev <jhmiller001_at_yahoo.com> wrote:
> >
> >>
> >> I am using the same spec and am having difficulties with JAXB. I would
> >> like
> >> to simply specify a String for the "xsd:any" content
> >>
> >> JAXB generates
> >>
> >> @XmlAccessorType(XmlAccessType.FIELD)
> >> @XmlType(name = "", propOrder = {
> >> "any"
> >> })
> >> public static class Message {
> >>
> >> @XmlAnyElement(lax = true)
> >> protected Object any;
> >>
> >> public Object getAny() {
> >> return any;
> >> }
> >>
> >> public void setAny(Object value) {
> >> this.any = value;
> >> }
> >>
> >> }
> >>
> >>
> >> And I would simply like to pass:
> >>
> >> message.setAny("hello world");
> >>
> >> Unfortunately, I receive the cannot marshall a java.lang.String object
> >> since
> >> it doesn't have an XmlRootElement.
> >>
> >> I tried the solution in this post but it did not seem to solve my
> >> problem.
> >> Does anyone have a relatively non invasive means of allowing this? I
> >> would
> >> prefer not hacking the OASIS spec, but my understanding is that you
> >> cannot
> >> replace complex types in JAXB using the bindings?
> >>
> >> Creating a new JAXBElement as follows
> >>
> >> JAXBElement<String> jaxb1 = new JAXBElement<String>(new
> >> QName("http://docs.oasis-open.org/wsn/b-2",""), String.class, "hello
> >> world");
> >>
> >> also doesn't work for me since I am wrapping my content with another set
> >> of
> >> XML tags that have nothing to do with the spec or my message.
> >>
> >> Thanks in advance for any help!
> >>
> >>
> >> Wolfgang Laun-2 wrote:
> >> >
> >> > Additional info:
> >> >
> >> > The prefix xjc must be bund like this:
> >> > xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
> >> >
> >> > Enclose the binding in a binding/schemaLocation element:
> >> > <jxb:bindings schemaLocation="someName.xsd">
> >> > <jxb:bindings node="//xs:any">
> >> > <xjc:dom/>
> >> > </jxb:bindings>
> >> > </jxb:bindings>
> >> >
> >> > -W
> >> > On Fri, Apr 3, 2009 at 7:32 AM, Gary Gregory
> >> > <GGregory_at_seagullsoftware.com>wrote:
> >> >
> >> >> Alejandro:
> >> >>
> >> >>
> >> >>
> >> >> You need to create an XJC binding file which should contain a
> fragment
> >> >> like:
> >> >>
> >> >>
> >> >>
> >> >> <jaxb:bindings node="//xs:any">
> >> >>
> >> >> <xjc:dom/>
> >> >>
> >> >> </jaxb:bindings>
> >> >>
> >> >>
> >> >>
> >> >> Gary
> >> >>
> >> >>
> >> >> ------------------------------
> >> >>
> >> >> *From:* Alejandro Escalante Medina [mailto:alex.escalante_at_gmail.com]
> >> >> *Sent:* Thursday, April 02, 2009 7:40 PM
> >> >> *To:* users_at_jaxb.dev.java.net
> >> >> *Subject:* JAXB and xsd:any values
> >> >>
> >> >>
> >> >>
> >> >> Hello guys, I am working with jax-ws and jaxb to implement WS-I
> >> >> Notification services.
> >> >>
> >> >> Everything seems to be working except for a small but important
> >> problem:
> >> >>
> >> >> The WS-I Notification schema specification defines the Message
> element
> >> as
> >> >> being able to contain <any> content. I understand that can be any
> >> >> well-formed xml. However, when unmarshaling inside my web service,
> the
> >> >> getAny() method for the message object comes out null every time...
> >> >>
> >> >> Any ideas?
> >> >>
> >> >> The xsd for WS-I Notification is here:
> >> >>
> >> >> http://docs.oasis-open.org/wsn/b-2.xsd
> >> >>
> >> >> I am using this command line to generate types:
> >> >>
> >> >> xjc.sh http://docs.oasis-open.org/wsn/b-2.xsd
> >> >>
> >> >>
> >> >> Thanks in advance...
> >> >>
> >> >> Alejandro Escalante Medina
> >> >> Visita mi página personal en http://weblocked.blogsome.com/
> >> >>
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/JAXB-and-xsd%3Aany-values-tp22860952p23285583.html
> >> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> >> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/JAXB-and-xsd%3Aany-values-tp22860952p23295855.html
> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>