Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/UserGuide/MOXy/Overview/DynamicEntities"
Line 87: | Line 87: | ||
{{tip||XML names found in the metadata (complex type names, element names, attribute names) will be translated to Java identifiers according to the algorithms described in "Appendix D: Binding XML Names to Java Identifiers" of the ''Java Architecture for XML Binding (JAXB) 2.2 Specification''. In the example above, "last-name" in XML was translated to "lastName" for the Java object.}} | {{tip||XML names found in the metadata (complex type names, element names, attribute names) will be translated to Java identifiers according to the algorithms described in "Appendix D: Binding XML Names to Java Identifiers" of the ''Java Architecture for XML Binding (JAXB) 2.2 Specification''. In the example above, "last-name" in XML was translated to "lastName" for the Java object.}} | ||
− | [http://jcp.org/en/jsr/detail?id=222 | + | [http://jcp.org/en/jsr/detail?id=222 Java Architecture for XML Binding (JAXB) 2.2 Specification] |
Revision as of 16:01, 29 June 2011
EclipseLink MOXy
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Contents
Static and Dynamic Entities
There are two high-level ways to use EclipseLink JAXB; Using pre-existing Java classes (Static MOXy), or using EclipseLink-generated in-memory Java classes (Dynamic MOXy).
Using Static MOXy
The most common way to use EclipseLink JAXB is with existing Java classes, mapped to XML using Java annotations and/or EclipseLink OXM metadata. These classes might be ones that you have written yourself, or they could be generated from an XML Schema using the XJC compiler tool.
Using this approach, you will be dealing with your actual domain objects when converting to and from XML. The following example shows a simple Java class that can be used with JAXB:
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Customer { @XmlAttribute private long id; private String name; // ... // get() and set() methods // ... }
The code below demonstrates how to unmarshal, modify, and marshal an object using static JAXB:
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class, Address.class); Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc); Address address = new Address(); address.setStreet("1001 Fleet St."); customer.setAddress(address); jaxbContext.createMarshaller().marshal(customer, System.out);
Using Dynamic MOXy
With EclipseLink Dynamic MOXy, you can bootstrap a JAXBContext from a variety of metadata sources and use existing JAXB APIs to marshal and unmarshal data… without having compiled Java class files on the classpath. This allows you to alter the metadata as needed, without having to update and recompile the previously-generated Java source code.
You should consider using Dynamic MOXy when:
- You want EclipseLink to generate classes from an XML schema (XSD).
- You do not want to deal with concrete Java domain classes.
Dynamic Entities
Instead of using actual Java classes (such as Customer.class or Address.class), Dynamic MOXy uses a simple get(propertyName) / set(propertyName, propertyValue) API to manipulate data. EclipseLink generates (in memory) a DynamicType associated with each DynamicEntity.
The code below demonstrates how to unmarshal, modify, and marshal an object using dynamic JAXB:
DynamicJAXBContext dynamicJAXBContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, myClassLoader, null); DynamicEntity customer = (DynamicEntity) dynamicJAXBContext.createUnmarshaller().unmarshal(instanceDoc); String lastName = customer.get("lastName"); List orders = customer.get("orders"); ... DynamicEntity address = dynamicJAXBContext.newDynamicEntity("mynamespace.Address"); address.set("street", "1001 Fleet St."); customer.set("lastName", lastName + "Jr."); customer.set("address", address); dynamicJAXBContext.createMarshaller().marshal(customer, System.out);