JAXB minOccurs = 0. Does the item exist 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 automatically generate my classes (e.g. Person.java, etc.).

So, at runtime, I use JAXB to untie the above XML message and get an instance of the Person object. When I do p.getLat()or p.getLon(), the return values ​​are 0.0, although the source XML does not contain <lat>or elements <lon>.

What's even worse is that 0.0, 0.0 is the actual latitude and longitude. It is rare for a person to be located there, but it is close to the point!

IBM XML- , , . .

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

, XML- :

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

. JAXB , , getLat(), getLon()?

+2
2

. xjc Person lat lon Double.

unmarshall XML <lat> <lon>, Person null , .

, 0.0 .

XML-:

<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>

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());
  }
}

foo.xml:

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

:

Fred
1.0
null
+4

0.0 vs null Double primitive Double object. 0.0, null, null . . Person, , .

0

All Articles