This will return us XML in the [[#Flickr XML Format|following format]]. For each '''entry''', we want to locate the '''link''' element that has '''@type='image/jpeg'''', and extract the '''href''' attribute, pointing to the actual image.

+

This will return us XML in the [[#Flickr XML Format|following format]]. For each '''entry''', we want to locate two '''link''' elements, based on their attributes:

+

+

* '''link''' with '''@type='image/jpeg'''', and extract the '''href''' attribute, pointing to the actual image

+

* '''link''' with '''@rel='alternate'''', and extract the '''href''' attribute, pointing to the corresponding Flickr page

==Mapping to Flickr XML==

==Mapping to Flickr XML==

−

The MOXy metadata that defines our mappings to Flickr's XML output is stored in the '''bindings-flickr.xml''' file. We define a '''FlickrResults''' class that will hold a collection of '''FlickrItem''' objects, as well as a '''description''' of the search being performed. Each '''FlickrItem''' simply contains a URL string pointing to an image file.

+

The MOXy metadata that defines our mappings to Flickr's XML output is stored in the '''bindings-flickr.xml''' file. We define a '''FlickrResults''' class that will hold a collection of '''FlickrItem''' objects, as well as a '''description''' of the search being performed. Each '''FlickrItem''' contains two URLs: one to the actual image, and one to the Flickr page for that image..

Dynamic MOXy: Flickr Example

This example will demonstrate how to use MOXy's Dynamic JAXB features to work with public JSON and XML feeds, without having to create concrete Java classes. The following concepts are demonstrated:

Mapping to JSON and XML data without writing or generating concrete Java classes

Using multiple MOXy metadata files to modularize your metadata

Bootstrapping a DynamicJAXBContext from multiple MOXy metadata files

Using a single DynamicJAXBContext to read both JSON and XML data

Using DynamicEntity APIs to interact with mapped data

Using UnmarshallerProperties.JSON_INCLUDE_ROOT to read JSON that does not have a root element

Using Marshaller.JAXB_FRAGMENT to omit the XML preamble when writing

Using XPath Predicate mapping to map to an element based on an attribute value

This example uses Dynamic MOXy to read a JSON stream from Reddit, an XML stream from Flickr, and uses the data from both to create an HTML file.

Running the Example

The full code for this example is available under moxy/dynamic/flickr in the EclipseLink Examples Git repository:

git clone git://git.eclipse.org/gitroot/eclipselink/examples.git

To run the example, just execute Maven:

$ mvn

This will connect to the Internet to read the JSON and XML feeds, and will launch the system browser on the generated HTML file.

Proxy ConnectionsIf you require a proxy server to access the Internet, check the <systemProperties> section of pom.xml.

Background: DynamicEntities

Instead of using actual Java classes (such as Customer.class or Address.class), Dynamic MOXy generates (in-memory) DynamicTypes defined by the metadata provided. An "instance" of a DynamicType is called a DynamicEntity, which provides a simple get(propertyName) / set(propertyName, propertyValue) API to manipulate data.

For more information, and to see other ways that Dynamic MOXy can be used, see Using Dynamic JAXB in the EclipseLink documentation.

Getting JSON from Reddit

This will return us JSON in the following format. For this example, we are primarily interested in the title and url fields of data/children/data.

Mapping to Reddit JSON

The MOXy metadata that defines our mappings to the JSON output is stored in the bindings-reddit.xml file. We define a RedditResults class that will hold a collection of RedditPost objects. Each RedditPost maps to the title and url returned by Reddit. Here, we are essentially designing virtual "classes" that will be mapped to the information we are interested in.

Getting XML from Flickr

This will return us XML in the following format. For each entry, we want to locate two link elements, based on their attributes:

link with @type='image/jpeg', and extract the href attribute, pointing to the actual image

link with @rel='alternate', and extract the href attribute, pointing to the corresponding Flickr page

Mapping to Flickr XML

The MOXy metadata that defines our mappings to Flickr's XML output is stored in the bindings-flickr.xml file. We define a FlickrResults class that will hold a collection of FlickrItem objects, as well as a description of the search being performed. Each FlickrItem contains two URLs: one to the actual image, and one to the Flickr page for that image..

readRedditPosts()

To unmarshal Reddit's JSON, we create a new Unmarshaller, and specify that our media type is JSON.

We also specify (JSON_INCLUDE_ROOT, false), because there is no root element in this JSON format. Because of this, we need to use an unmarshal() method that takes a Class parameter, indicating the type of object we are unmarshalling. We can obtain our "virtual" class by creating a new DynamicEntity of type RedditResults.

findFlickrResults()

Now that we have RedditResults, we will iterate over its RedditPosts and construct a Flickr query URL for each one, using keywords from the post's title. We'll then unmarshal from the query URL to get an instance of our FlickrResults. Because Flickr is providing a standard XML format, a simple default Unmarshaller is all that is necessary.

writeHtml()

Finally, we take the information from our RedditResults and FlickerResults and put it together into an HtmlPage object, defined in bindings-html.xml. We then marshal this to a File. In addition to specifying that our Marshaller should use formatted output, we also use (Marshaller.JAXB_FRAGMENT, true), to omit the <?xml ... preamble from our HTML file.