users@jaxb.java.net

Re: accessing _has elements

From: Jon Richards <jonrichards_at_onetel.net.uk>
Date: Wed, 10 Dec 2003 19:16:00 -0000

MessageMany thanks, I'll give this a try when I'm back at work tomorrow.

Regards,

Jon.
  ----- Original Message -----
  From: marcus.walls_at_aspective.com
  To: users_at_jaxb.dev.java.net
  Sent: Wednesday, December 10, 2003 9:01 AM
  Subject: RE: accessing _has elements


  I use the isSet functionality to achieve this. I place the following at the top of my XSD:

    <xsd:annotation>
      <xsd:appinfo>
        <jxb:globalBindings generateIsSetMethod="true"/>
      </xsd:appinfo>
    </xsd:annotation>

  If I have an element X then JAXB will generate an isSetX method that returns a boolean determining whether the element/attribute was explicitly set or not.

  There is a distinction between whether an element/attribute has been set and whether or not it is null. Depending upon your data/application it may be equivalent for you to regard a null object as an element/attribute that has not been set, but it is possible to explicitly set an element/attribute to null so you need to be careful to make this distinction if your application needs it.

  I've probably not made that very clear, so let me give an example! If I have two xsd:string elements A and B without any defaults and I create the object via ObjectFactory, then getA(), getB(), isSetA(), and isSetB() will return null, null, false, and false respectively. (No surprises there).

  However, if I then call setA(null), then getA(), getB(), isSetA(), and isSetB() will return null, null, true, and false respectively - not particularly surprising when you think about it, but can catch you out if you're not careful!

  Hope that helps and makes sense to someone!

  Marcus


  -----Original Message-----
  From: Jon Richards [mailto:jonrichards_at_onetel.net.uk]
  Sent: Wednesday, December 10, 2003 7:36 AM
  To: users_at_jaxb.dev.java.net
  Subject: accessing _has elements


  Appologies if this is not the correct forum for this question.

  I have generated classes for a schema where some elements are
  minoccurs="0".

  After unmarshalling I would like to know whether the element
  was present or not. Objects are fine as they return null, but
  base types like int will return 0.

  Unfortunately the 'has' attributes in the impl classes are
  protected, and the type interface does not provide access
  methods for these attributes.

  Is there a way of finding out whether elements were present
  or not? If not is it appropriate for the next JAXB release to
  add access methods for the 'has' methods?

  Regards,

  Jon Richards.