Turning any XML document into a Java Object graph using JAXB 2.0

0

As a followup to my recent article on how to Produce and XML document based on data in a POJO structure using JAXB (http://technology.amis.nl/blog/12499/creating-an-xml-document-based-on-my-pojo-domain-model-how-will-jaxb-help-me) I will describe in this article how to go the reverse route: how to take any XML document and process it into a Java Object graph. I will do that using JAXB.

The steps described in this article are:

  • create XSD based on the XML document that I want to turn into Java objects
  • generate a JAXB Java class structure based on the XSD
  • use JAXB to unmarshall the XML document from file or URL into the Java objects
  • retrieve information from the POJOs that are instantiated by the unmarshalling

The tool I use in this article is JDeveloper 11g. However, everything is based on standard technology and will work anywhere (except for the creation of the XSD based on an XML, that is a specific XML IDE feature).

The starting point in this article could be any XML document you can think of. I am picking an RSS feed as an example – because it is an omnipresent type of XML document that you may want to process programmatically. Then again, there are various libraries that can process RSS (for example Rome) – so maybe RSS is not the most useful example after all.

Anyways, here is an example of the XML produced by an RSS feed – the RSS feed from this very blog:

Image

The resulting XML looks like this:

Image

I turn this into an XML document on my local file system, including it in my JDeveloper project:

Image

Create XSD Schema Definition for XML Document(s)

The next step is to create an XSD schema definition that describes RSS documents like this one (and others as well). Fortunately, JDeveloper 11g has a very useful feature that automatically creates and XSD document from an instance XML document. This XSD may not accurately describe the whole range of XML documents, but at least it describes the source XML.

Open the New Gallery and pick XML Schema from XML Document. Press OK.

Image

The following popup appears where you can configure some of the XSD settings, like its name and destination directory and its primary namespace. You can also select the XML document that is to be used to create the XSD document for:

Image

Press OK to have the XSD generated:

Image

Generate JAXB based Classes from XSD Schema Definition

JDeveloper has an easy entrance for the JAXB Schema Compiler – the tool that creates a Java Class structure that mimics the structure and types of the XSD definition. Again, from the New Gallery, pick the option JAXB 2.0 Content Model from XML Schema in the Category Business Tier | Toplink/JPA:

Image

Press OK. The popup window has me specify the XSD document, the Java package into which the classes are to be generated and optional customization settings (for example to influence the names of the generated classes).

Image

Press OK to generate the JAXB Classes.

Image

When done, the result is initially slightly disappointing:

Image

Only a single class seems to have been generated. Even though we know that the source XML document has deeper nested structures as is also defined in the XSD. Where is the class for the Channel and Item elements?

On closer inspection, it turns out that the Rss.java file contains several (inner) public static classes:

Image

so apparently everything is alright after all.

By the way: the JDeveloper project makes use of two libraries (associated with the project through the last wizard operation):

Image

Unmarshall XML document into Java object graph

The final step in this article is the actual unmarshalling of an RSS feed based XML document into a Java Object graph. This only requires a few lines of code, now that we have our generated RSS class (with inner classes):

public class RssToJavaProcessor {

    public static void main(String[] args) throws JAXBException, SAXException {
        JAXBContext jaxbctx = JAXBContext.newInstance("org.rss");
        Unmarshaller unm = jaxbctx.createUnmarshaller();
        File xml = new File("C:/Temp/ConsumeXMLusingJAXB/ConsumeRSSintoJava/AMISBlogRSSFeed.xml");
        Rss rss = (Rss)unm.unmarshal(xml);
        System.out.println(rss.getChannel().getDescription());
        for (Rss.Channel.Item i : rss.getChannel().getItem()) {
            System.out.println(i.getTitle()+" - "+i.getPubDate());
        }
    }
}

The result of running this code for the current RSS feed of our blog is:

Image

Resources

Download JDeveloper project with source code for this article: ConsumeRSSXMLusingJAXB.

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

Leave a Reply