Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/MOXy/Dynamic/DynamicEntityToXml"

< EclipseLink‎ | Examples‎ | MOXy‎ | Dynamic
(Create Dynamic Entities)
(Marshal Dynamic Entities to XML)
 
(3 intermediate revisions by the same user not shown)
Line 3: Line 3:
  
 
==Create Dynamic Entities==
 
==Create Dynamic Entities==
 +
 +
The DynamicJAXBContext is used to create instances of DynamicEntity.  The entity and property names correspond to the class and property names that would have been generated if static JAXB had been used.
 +
 
<source lang="java">
 
<source lang="java">
 
DynamicEntity customer = jaxbContext.newDynamicEntity("org.example.Customer");
 
DynamicEntity customer = jaxbContext.newDynamicEntity("org.example.Customer");
Line 8: Line 11:
  
 
DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
 
DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
address.set("street", "1 Any Street");
+
address.set("street", "1 Any Street").set("city", "Any Town");
address.set("city", "Any Town");
+
 
customer.set("address", address);
 
customer.set("address", address);
 
</source>
 
</source>
  
 
==Marshal Dynamic Entities to XML==
 
==Marshal Dynamic Entities to XML==
 +
The Marshaller obtained from the DynamicJAXBContext is a standard marshaller, and can be used normally to marshal instances of DynamicEntity.
 
<source lang="java">
 
<source lang="java">
 
Marshaller marshaller = jaxbContext.createMarshaller();
 
Marshaller marshaller = jaxbContext.createMarshaller();

Latest revision as of 12:08, 24 June 2010

Overview

In this example we will show how to create dynamic entities and marshal then to XML.

Create Dynamic Entities

The DynamicJAXBContext is used to create instances of DynamicEntity. The entity and property names correspond to the class and property names that would have been generated if static JAXB had been used.

DynamicEntity customer = jaxbContext.newDynamicEntity("org.example.Customer");
customer.set("name", "Jane Doe");
 
DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
address.set("street", "1 Any Street").set("city", "Any Town");
customer.set("address", address);

Marshal Dynamic Entities to XML

The Marshaller obtained from the DynamicJAXBContext is a standard marshaller, and can be used normally to marshal instances of DynamicEntity.

Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(customer, System.out);

Back to the top