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/XmlToDynamicEntity"

< EclipseLink‎ | Examples‎ | MOXy‎ | Dynamic
(XML Document)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==Overview==
 
==Overview==
 
In this example you will learn how to unmarshal dynamic entities from XML.
 
In this example you will learn how to unmarshal dynamic entities from XML.
 +
 +
==Unmarshal DynamicEntities from XML==
 +
The Unmarshaller obtained from the DynamicJAXBContext is a standard unmarshaller, and can be used normally to unmarshal instances of DynamicEntity.
 +
<source lang="java">
 +
FileInputStream xmlInputStream = new FileInputStream("src/example/dynamic/customer.xml");
 +
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
 +
DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(xmlInputStream);
 +
</source>
 +
 +
==Get Data from the DynamicEntity==
 +
DynamicEntity offers property based data access, get("name") instead of getName().
 +
<source lang="java">
 +
System.out.println(customer.<String>get("name"));
 +
</source>
 +
 +
==Get Data using DynamicType==
 +
Instances of DynamicEntity have a corresponding DynamicType.  DynamicType can be used to introspec the DynamicEntity.
 +
<source lang="java">
 +
DynamicType addressType = jaxbContext.getDynamicType("org.example.Address");
 +
 +
DynamicEntity address = customer.<DynamicEntity>get("address");
 +
for(String propertyName: addressType.getPropertiesNames()) {
 +
    System.out.println(address.get(propertyName));
 +
}
 +
</source>
 +
 +
==XML Document==
 +
Below is a sample XML document for this example.
 +
<source lang="xml">
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<customer xmlns="www.example.org">
 +
  <name>Jane Doe</name>
 +
  <address>
 +
      <street>1 Any Street</street>
 +
      <city>Any Town</city>
 +
  </address>
 +
</customer>
 +
</source>
 +
 +
==Next Steps==
 +
Next we will learn how to marshal instances of DynamicEntity to XML, see[[EclipseLink/Examples/MOXy/Dynamic/DynamicEntityToXml|DynamicEntity to XML]].

Latest revision as of 12:24, 24 June 2010

Overview

In this example you will learn how to unmarshal dynamic entities from XML.

Unmarshal DynamicEntities from XML

The Unmarshaller obtained from the DynamicJAXBContext is a standard unmarshaller, and can be used normally to unmarshal instances of DynamicEntity.

FileInputStream xmlInputStream = new FileInputStream("src/example/dynamic/customer.xml");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(xmlInputStream);

Get Data from the DynamicEntity

DynamicEntity offers property based data access, get("name") instead of getName().

System.out.println(customer.<String>get("name"));

Get Data using DynamicType

Instances of DynamicEntity have a corresponding DynamicType. DynamicType can be used to introspec the DynamicEntity.

DynamicType addressType = jaxbContext.getDynamicType("org.example.Address");
 
DynamicEntity address = customer.<DynamicEntity>get("address");
for(String propertyName: addressType.getPropertiesNames()) {
    System.out.println(address.get(propertyName));
}

XML Document

Below is a sample XML document for this example.

<?xml version="1.0" encoding="UTF-8"?>
<customer xmlns="www.example.org">
   <name>Jane Doe</name>
   <address>
      <street>1 Any Street</street>
      <city>Any Town</city>
   </address>
</customer>

Next Steps

Next we will learn how to marshal instances of DynamicEntity to XML, seeDynamicEntity to XML.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.