xml-xmlbeans-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From MIDON ALEXIS <ALEXIS.MI...@sgam.com>
Subject RE: XmlValueNotNillableException
Date Wed, 28 Jul 2004 08:22:11 GMT





This is a bit tiresome!


If the value is null and the tag optional why xmlbeans does not skip it
instead og throw an exception?


Moreover this exception does not seem to be systematic.
In the same way described in my first mail, I set some optional flag of
<contact> element and no exceptions are thrown.

Sample code :

Contact contact = Contact.Factory.newInstance();

// if the first name is null, xmlbeans doest not thrown any exceptions...
contact.setFirstName(contactDTO.getFirstName());
(...)

AddressInfo address = AddressInfo.Factory.newInstance();
// ...whereas this optional field cannot be null 
address.setCityName(addressDTO.getCity());
(...)

 

I have checked the corresponding xsd files and the Address & Contact are
defined as following :


<xs:complexType name="Contact">
                <xs:sequence>
                        <xs:element name="firstName" type="xs:string"
minOccurs="0"/>
                        <xs:element name="lastName" type="xs:string"
minOccurs="0"/>
                        (...)
                        <xs:element name="address" type="AddressInfo"
minOccurs="0"/>
                </xs:sequence>
</xs:complexType>
<xs:complexType name="AddressInfo">
                <xs:sequence>
                        <xs:element ref="cityName" minOccurs="0"/>
                        (...)
                </xs:sequence>
</xs:complexType>
<xs:element name="cityName">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:maxLength value="35"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>


The only difference I find out is that the "cityName" element is a global
element whereas "firstName" element is local.
Is this a good reason ?




-----Message d'origine-----
De : Kevin Krouse [  <mailto:kkrouse@bea.com> mailto:kkrouse@bea.com]
Envoye : mardi 27 juillet 2004 21:04
A : xmlbeans-user@xml.apache.org
Objet : RE: XmlValueNotNillableException


Yes, you should check for null before setting it.  Setting a value of
null is equivalent to having xsi:nil="true" on the element.

________________________________

From: MIDON ALEXIS [  <mailto:ALEXIS.MIDON@sgam.com>
mailto:ALEXIS.MIDON@sgam.com]
Sent: Tuesday, July 27, 2004 9:53 AM
To: 'xmlbeans-user@xml.apache.org'
Subject: XmlValueNotNillableException



Hello,

my schema defines an AddressInfo element. All the sub elements
(streetname, cityname, etc.) are optional.

I map this element with an AddressDTO as shown in the following code :


public static AddressInfo createAddress(AddressDTO dto) {
      AddressInfo address = AddressInfo.Factory.newInstance();


      address.setStreetName(dto.getAddress());
      address.setCityName(dto.getCity());
      address.setPostalCode(dto.getZipCode());

      return address;
 }

To that point I make no assumptions on the dto field value, ie the
getters could return null (this is the reason why tags are optional)

Unfortunately this code throws an unknown (i didn't find out any
javadocs) and unchecked exception :
org.apache.xmlbeans.impl.values.XmlValueNotNillableException

My biz fails due to this exception. :(

Is it a bug? a bad practice? Did I miss something?

Should I test every nullable value before setting the XMLObject ?

ex :
      if(dto.getAddress()!=null)
            address.setStreetName(dto.getAddress());
      if(dto.getCityName()!=null)
          address.setCityName(dto.getCity());
      if(dto.getPostalCode()!=null)
          address.setPostalCode(dto.getZipCode());



Thx for your help.


Alexis

- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-user-help@xml.apache.org
Apache XMLBeans Project -- URL:  <http://xml.apache.org/xmlbeans/>
http://xml.apache.org/xmlbeans/



Mime
View raw message