Posts tagged jaxb
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 challenge is a common one. We have got data in a domain model, based on POJOs. And we need to marshall that data into an XML document. Note that the exact structure of that document is not so important – we will probably translate is somewhere anyway. At this point, all we have is a set of POJO definitions. There is no XSD, no predefined XML structure.
JAXB – the Java Architecture for XML Binding – seems like the technology to turn to. My experience with JAXB has mainly been from the other end: we have XML data, described by an XSD, that we want to turn into Java Objects (unmarshall XML to Java). In that case, we take the XSD that describes the XML, generate Java classes and use JAXB to unmarshall XML into objects based on those classes. The alternative round – taking classes and marshalling them into XML would be just as simple – or even simpler, now would it not?
Well, it was not as straightforward as I hoped it would be. I thought that adding a few JAXB annotations to my existing POJO model would do the trick – but no such luck. Unless I am completely misguided and mistaken, the steps required are:
- create an XSD based on the original POJO model
- generate JAXB annotated More >
A few months ago I came across a relatively new book: Processing XML Documents with Oracle JDeveloper 11g by Deepak Vohra (370 pages, Packt Publishing, ISBN 978-1-847196-66-8, February 2009).
It is an interesting mix of topics, all having to do with XML and most directly related to JDeveloper. The topcis and chapters do not at all times seem logically bundled together (for example design time and run time seem to be somewhat strangely intermingled in the book) but they provide a lot of useful information to any developer working on applications that involve XML in some way (and which one does not today) using JDeveloper as an IDE or Oracle XDK 11g..
And JDeveloper 11g’s XML capabilities may not be entirely on par with single issue IDEs such as XMLSpy, it certainly does a very good job at many frequent and less frequent XML tasks. This book does a good job at showing the various XML specific features of JDeveloper – although it also fails to mention one or two. It contains many examples of writing Java code to process XML in some way, primarily using XDK 11g; those examples are not always really specifically related to JDeveloper 11g, as the code uses standard libraries that can More >