Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Type Level/Mapping to a Type or Element/Default Root Element"

(Default Root Element)
Line 66: Line 66:
  
 
When an instance of the <tt>Customer</tt> class is persisted to XML, the EclipseLink runtime performs the following:
 
When an instance of the <tt>Customer</tt> class is persisted to XML, the EclipseLink runtime performs the following:
<ol>
+
 
<li> Gets the default root element.The <tt>Customer</tt> class instance corresponds to the root of the XML document. The EclipseLink runtime uses the default root element specified on the descriptor (<tt>customer</tt>) to start the XML document. EclipseLink then uses the mappings on the descriptor to marshal the object's attributes:
+
# Gets the default root element. The <tt>Customer</tt> class instance corresponds to the root of the XML document. The EclipseLink runtime uses the default root element (<tt>customer</tt>) specified in either annotations or OXM to start the XML document. EclipseLink then uses the mappings on the class to marshal the object's attributes.
<div class="pre">
+
<source lang="xml">
<customer>
+
<customer>
    <name></name>
+
  <name>...</name>
</customer>
+
</customer>
</div>
+
# When the EclipseLink runtime encounters an object attribute such as <tt>billingAddress</tt>, it checks the mapping associated with it to determine with what element (<tt>billing-address</tt>) to continue.
</li>
+
<source lang="xml">
<li> When the EclipseLink runtime encounters an object attribute such as <tt>billingAddress</tt>, it checks the mapping associated with it to determine with what element (<tt>billing-address</tt>) to continue:
+
<customer>
<div class="pre">
+
  <name>...</name>
<customer>
+
  <billing-address/>
    <name></name>
+
</customer>
    <billing-address/>
+
</source>
</customer>
+
# The EclipseLink runtime checks the mapping's reference descriptor (<tt>Address</tt>) to determine what attributes to persist:  
</div>
+
<source lang="xml">
<br>
+
  <customer>
The EclipseLink runtime checks the mapping's reference descriptor (<tt>Address</tt>) to determine what attributes to persist:  
+
      <name>...</name>
<div class="pre">
+
      <billing-address>
<customer>
+
         <street>...</street>
    <name></name>
+
         <city>...</city>
    <billing-address>
+
      </billing-address>
         <street></street>
+
  </customer>
         <city></city>
+
</source>
+
    </billing-address>
+
</customer>
+
</div>
+
</li>
+
</ol>
+
  
 
{{EclipseLink_Note
 
{{EclipseLink_Note

Revision as of 15:45, 6 January 2011

EclipseLink MOXy

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

Elug api package icon.png Key API

Default Root Element

At least one of your mapped classes must have a default root element defined. This tells EclipseLink what the top-level root of your XML document will be. Consider the Customer and Address classes shown in this example:

Defaultrootelement.png

These classes correspond to the XML schema shown in this example. The schema contains a top-level element of type "customer-type", therefore our Customer class will need to have a default root element specified.

<xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="address-type">
      <xsd:sequence>
         <element name="street" type="xsd:string"/>
         <element name="city" type="xsd:string"/>
      </xsd:sequence>
   </xsd:complexType>
 
   <xsd:element name="customer" type="customer-type"/>
 
   <xsd:complexType name="customer-type">
      <xsd:sequence>
         <xsd:element name="name" type="xsd:string"/>
         <xsd:element name="billing-address" type="address-type"/>
         <xsd:element name="shipping-address" type="address-type"/>
      </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

The following example shows how to annotate your Java class to specify a default root element. All that is needed is the standard JAXB @XmlRootElement annotation.

@XmlRootElement
public class Customer {
   private String name;
   private Address billingAddress;
   private Address shippingAddress;
 
   ...
}

The example below shows how specify a default root element in EclipseLink's OXM metadata format.

...
<java-type name="Customer">
   <xml-root-element name="customer"/>
   <java-attributes>
      <xml-element java-attribute="name"/>
      <xml-element java-attribute="billingAddress" name="billing-address"/>
      <xml-element java-attribute="shippingAddress" name="shipping-address"/>
   </java-attributes>
</java-type>
...

When an instance of the Customer class is persisted to XML, the EclipseLink runtime performs the following:

  1. Gets the default root element. The Customer class instance corresponds to the root of the XML document. The EclipseLink runtime uses the default root element (customer) specified in either annotations or OXM to start the XML document. EclipseLink then uses the mappings on the class to marshal the object's attributes.
<customer>
   <name>...</name>
</customer>
# When the EclipseLink runtime encounters an object attribute such as <tt>billingAddress</tt>, it checks the mapping associated with it to determine with what element (<tt>billing-address</tt>) to continue.
<source lang="xml">
<customer>
   <name>...</name>
   <billing-address/>
</customer>
  1. The EclipseLink runtime checks the mapping's reference descriptor (Address) to determine what attributes to persist:
   <customer>
      <name>...</name>
      <billing-address>
         <street>...</street>
         <city>...</city>
      </billing-address>
   </customer>

Elug note icon.png

Note: The undefined document root element of a referenced object is ignored during marshalling with an any collection mapping and object mapping.


Eclipselink-logo.gif
Version: 2.2.0
Other versions...