tags:

views:

557

answers:

2

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()?

+3  A: 

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"&gt;
 <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
Joachim Sauer
I just tried your example and it works as you suggested. This is good news! It's not a limitation of JAXB but a problem with my project. Thankyou!
leif81
A: 

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.

Joe Blow
Yes I think you are right about the double vs. Double. However the problem seems to stem from JAXME converting xsd:double to java primitive double and Sun Java JAXB converting a xsd:double to a java Double. The JAXME gererated code provides no way to check if the <lat> <lon> values in the XML were set. Checking for 0.0 is not desirable because 0.0 is a valid value for latitude and longitude. I have switched to using JAXB so that I can check for null to determine if the element was provided.
leif81