Skip to main content
Jump to: navigation, search

Dynamic JAXB: 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 EclipseLink binding files to modularize your metadata
  • Bootstrapping a DynamicJAXBContext from multiple EclipseLink 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 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://

To run the example, just execute Maven:


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 Connections
If you require a proxy server to access the Internet, check the <systemProperties> section of pom.xml.

Getting JSON from Reddit

Reddit provides a public JSON feed using the following URL format:

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

    <java-type name="">
            <xml-element java-attribute="posts" xml-path="data/children" container-type="java.util.ArrayList"
                         type="" />                
    <java-type name="">
            <xml-element java-attribute="title" xml-path="data/title/text()" type="java.lang.String"/>                
            <xml-element java-attribute="url" xml-path="data/url/text()" type="java.lang.String"/>                

Getting XML from Flickr

Flickr provides a public XML feed using the following URL format:

This will return us XML in the 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.

Mapping to Flickr XML

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

<xml-schema element-form-default="QUALIFIED" namespace=""/>
    <java-type name="">
        <xml-root-element name="feed"/>
            <xml-element java-attribute="description" xml-path="title/text()" type="java.lang.String"/>                
            <xml-element java-attribute="items" xml-path="entry" container-type="java.util.ArrayList"
    <java-type name="">
            <xml-attribute java-attribute="imageUrl" xml-path="link[@type='image/jpeg']/@href" type="java.lang.String"/>                

Appendix - Data Formats

Reddit JSON Format

          "title":"Plants re-grow after 500 years under the ice",

Flickr XML Format

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="" xmlns:dc="" xmlns:flickr="urn:flickr:user" xmlns:media="">
  <title>Recent Uploads tagged science</title>
  <link rel="self" href="" />
  <link rel="alternate" type="text/html" href="" />
  <subtitle />
  <generator uri="">Flickr</generator>
    <title>Space Shuttle Endeavour</title>
    <link rel="alternate" type="text/html" href="" />
    <content type="html">&lt;p&gt;&lt;a href=""&gt;Jason Scheier&lt;/a&gt; posted a photo:</content>
      <name>Jason Scheier</name>
    <link rel="enclosure" type="image/jpeg" href="" />
    <category term="park" scheme="" />
    <category term="love" scheme="" />
    <category term="los" scheme="" />
    <category term="memorial" scheme="" />
    <category term="day" scheme="" />
    <category term="control" scheme="" />
    <category term="angeles" scheme="" />
    <category term="space" scheme="" />
    <category term="engineering" scheme="" />
    <category term="center" scheme="" />
    <category term="science" scheme="" />
    <category term="nasa" scheme="" />
    <category term="exposition" scheme="" />
    <category term="shuttle" scheme="" />
    <category term="mission" scheme="" />
    <category term="jpl" scheme="" />
    <category term="starship" scheme="" />
    <category term="endeavour" scheme="" />

Back to the top