Jump to: navigation, search

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

Line 12: Line 12:
 
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 <tt>Customer</tt> and <tt>Address</tt> classes shown in this example:
 
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 <tt>Customer</tt> and <tt>Address</tt> classes shown in this example:
  
 
+
[[Image:Defaultrootelement.png]]<br><br>
<span id="Example 14-1"></span>
+
''''' Customer and Address Classes'''''
+
'''<tt>Class:</tt>''' Customer
+
+
'''<tt>Default Root:</tt>''' customer
+
'''<tt>Attributes and Mappings:</tt>'''
+
    name:String                Direct Mapping to              name/text()
+
    billingAddress:Address    Composite Object Mapping to    billing-address
+
    shippingAddress:Address    Composite Object Mapping to    shipping-address
+
+
'''<tt>Class:</tt>''' Address
+
'''<tt>Default Root:</tt>''' address
+
'''<tt>Attributes and Mappings:</tt>'''
+
    street:String              Direct Mapping to              street/text()
+
    city:String                Direct Mapping to              city/text()
+
+
 
+
  
 
These classes correspond to the XML schema, shown in this example.
 
These classes correspond to the XML schema, shown in this example.
  
<span id="Example 14-2"></span>
 
''''' Customer and Address Schema'''''
 
 
<source lang="xml">
 
<source lang="xml">
<xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+
<xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsd:complexType name="address-type">
+
  <xsd:complexType name="address-type">
        <xsd:sequence>
+
      <xsd:sequence>
+
        <element name="street" type="xsd:string"/>
            <element name="street" type="xsd:string"/>
+
        <element name="city" type="xsd:string"/>
            <element name="city" type="xsd:string"/>
+
      </xsd:sequence>
        </xsd:sequence>
+
  </xsd:complexType>
    </xsd:complexType>
+
 
    <xsd:element name="customer" type="customer-type"/>
+
  <xsd:element name="customer" type="customer-type"/>
    <xsd:complexType name="customer-type">
+
 
+
  <xsd:complexType name="customer-type">
        <xsd:sequence>
+
      <xsd:sequence>
            <xsd:element name="name" type="xsd:string"/>
+
        <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="billing-address" type="address-type"/>
+
        <xsd:element name="billing-address" type="address-type"/>
            <xsd:element name="shipping-address" type="address-type"/>
+
        <xsd:element name="shipping-address" type="address-type"/>
        </xsd:sequence>
+
      </xsd:sequence>
    </xsd:complexType>
+
  </xsd:complexType>
+
</xsd:schema>
</xsd:schema>
+
 
</source>
 
</source>
  

Revision as of 14:32, 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.

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

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 specified on the descriptor (customer) to start the XML document. EclipseLink then uses the mappings on the descriptor to marshal the object's attributes:
    <customer>
        <name>…</name>
    </customer>
    
  2. When the EclipseLink runtime encounters an object attribute such as billingAddress, it checks the mapping associated with it to determine with what element (billing-address) to continue:
    <customer>
        <name>…</name>
        <billing-address/>
    </customer>
    


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