Difference between revisions of "EclipseLink/UserGuide/MOXy/Advanced XML Schema Concepts/Handling Null Values"

From Eclipsepedia

Jump to: navigation, search
m (Replacing page with ''''Warning This page is obsolete. Please see ''[http://www.eclipse.org/eclipselink/documentation/2.4/ Developing JAXB Applications Using EclipseLi...')
 
Line 1: Line 1:
{{EclipseLink_UserGuide
+
'''[[Image:Elug_draft_icon.png|Warning]] This page is obsolete. Please see ''[http://www.eclipse.org/eclipselink/documentation/2.4/ Developing JAXB Applications Using EclipseLink MOXy]'' for current information.'''
|eclipselink=y
+
|eclipselinktype=MOXy
+
|info=y
+
}}
+
 
+
=Working with Null Values=
+
 
+
Eclipselink offers several ways to configure its handling of null values in both Java and XML.  For example, your XML may use '''xsi:nil''' to represent a null value (e.g. '''<first-name xsi:nil="true"/>'''), or it might simply use an empty element (e.g. '''<first-name></first-name>''').  On the Java side, you may want to specify exactly how a null value should be written to XML ('''nil''' or empty node).  You can even specify an "isSet" method, to differentiate between values that were explicitly set to null versus values which are null due to being unset.
+
 
+
Consider the following schema:
+
 
+
<source lang="xml">
+
<?xml version="1.0" encoding="UTF-8"?>
+
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+
+
  <xsd:element name="customer" type="customer-type"/>
+
+
  <xsd:complexType name="customer-type">
+
      <xsd:element name="id" type="xsd:string"/>
+
      <xsd:element name="name" type="xsd:string"/>
+
      <xsd:element name="account-number" type="xsd:string" nillable="true"/>
+
  </xsd:complexType>
+
+
</xsd:schema>
+
</source>
+
 
+
An example instance document might look like:
+
 
+
<source lang="xml">
+
<customer>
+
  <id/>
+
  <name>Jon Smith</name>
+
  <account-number xsi:nil="true"/>
+
</customer>
+
</source>
+
 
+
For this example, we would like:
+
 
+
* Empty '''id''' tags to correspond to '''null''' in Java
+
* Empty '''name''' tags to correspond to "" (empty string) in Java (this is EclipseLink's default null-handling behavior)
+
* A '''nil''' '''account-number''' to correspond to '''null''' in Java
+
 
+
To achieve this behavior in EclipseLink, our mappings can be defined using annotations as follows:
+
 
+
<source lang="java">
+
package example;
+
 
+
import javax.xml.bind.annotation.*;
+
import org.eclipse.persistence.oxm.annotations.*;
+
 
+
@XmlRootElement
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class Customer {
+
  private String id;
+
 
+
  private String name;
+
 
+
  @XmlElement(name="account-number")
+
  @XmlNullPolicy(xsiNilRepresentNull="true" nullRepresentationForXml=XmlMarshalNullRepresentation.XSI_NIL)
+
  private String accountNumber;
+
 
+
  ...
+
}
+
</source>
+
 
+
Here, we have specified that '''xsi:nil''' in XML should represent '''null''' in Java, and conversely, that '''null''' in Java should be represented by '''xsi:nil''' in XML.
+
 
+
 
+
{{EclipseLink_MOXy
+
|version=2.2.0 DRAFT
+
|previous=[[EclipseLink/UserGuide/MOXy/Relationships/Privately Owned|Privately owned]]
+
|next=[[EclipseLink/UserGuide/MOXy/Relationships/Privately_Owned/One-to-Many|One-to-many]]
+
|up=[[EclipseLink/UserGuide/MOXy/Relationships/Privately Owned|Privately owned]]
+
}}
+

Latest revision as of 13:12, 30 January 2013

Warning This page is obsolete. Please see Developing JAXB Applications Using EclipseLink MOXy for current information.