English 中文(简体)
JAXB minOccurs=0. Element exists or not?
原标题:
  • 时间:2009-11-26 23:22:54
  •  标签:
  • java
  • xml
  • jaxb

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

最佳回答

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
问题回答

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签