leif.gruenwoldt
leif.gruenwoldt

Reputation: 13957

JAXB minOccurs=0. Element exists or not?

I have an XML schema:

<xsd:element name="Person">
 <xsd:complexType>
  <xsd:sequence>
   <xsd:element name="name" type="xsd:string" />
   <xsd:element name="lat" type="xsd:double" minOccurs="0"/>
   <xsd:element name="lon" type="xsd:double" minOccurs="0"/>
  </xsd:sequence>
 </xsd:complexType>
</xsd:element>

And I have an XML message:

<Person>
 <name>Fred</name>
</Person>

I use JAXB to auto-generate my classes (i.e. Person.java, etc).

So at run time I use JAXB to unmarshal the above XML message and get a Person object instance. When I do a p.getLat() or p.getLon() the return values are 0.0 even though the original XML didn't contain <lat> or <lon> elements.

What makes this worse is that 0.0, 0.0 is a valid latitude and longitude. It's rare for a person to be located there but that's beside the point!

An article on the IBM site suggested using an additional XML element as metadata to explicitly state whether the optional element exists or not. i.e.

<xsd:element name="hasLat" type="xsd:boolean"/>
<xsd:element name="hasLon" type="xsd:boolean"/>

So the XML message above would become:

<Person>
 <name>Fred</name>
 <hasLat>false</hasLat>
 <hasLon>false</hasLon>
</Person>

This seems like an ugly hack. There must be a proper way with JAXB to check if the element existed so that I can trust the return value from my getLat(), getLon()?

Upvotes: 2

Views: 6973

Answers (2)

Joe Blow
Joe Blow

Reputation: 1

The most likely reason for getting 0.0 returned vs null is the use of the Double primitive type or Double object type. The Double primitive will default to 0.0 if the value is null, since null is not a valid value for primitive types. The Double object will allow you to assign a null value to these fields. A peak at your Person class will probably reveal this.

Upvotes: 0

Joachim Sauer
Joachim Sauer

Reputation: 308001

I don't see that problem at all. For me xjc generates a Person class with the properties lat and lon with type Double.

If I unmarshall an XML file with no <lat> or <lon> elements, then the resulting Person objects has null values for those properties, as I'd expect.

I don't know how you get 0.0 anywhere.

My XML Schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.example.com/person">
 <xsd:element name="Person">
  <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="name" type="xsd:string" />
    <xsd:element name="lat" type="xsd:double" minOccurs="0"/>
    <xsd:element name="lon" type="xsd:double" minOccurs="0"/>
   </xsd:sequence>
  </xsd:complexType>
 </xsd:element>
</xsd:schema>

My Test.java:

import com.example.person.Person;
import javax.xml.bind.JAXB;
import java.io.File;

public class Test {
  public static void main(String[] args) {
    Person p = JAXB.unmarshal(new File("foo.xml"), Person.class);
    System.out.println(p.getName());
    System.out.println(p.getLat());
    System.out.println(p.getLon());
  }
}

My foo.xml:

<Person>
 <name>Fred</name>
 <lat>1.0</lat>
</Person>

Output:

Fred
1.0
null

Upvotes: 6

Related Questions