Posts tagged xsd

Creating an XML document based on my POJO domain model – how will JAXB help me?

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 classes from that XSD schema (classes that very similar to but not exactly the same as the original POJOs)
  • use the JAXB ObjectFactory to create instances of the JAXB-POJOs (rather than simply new Object() or new Object(a,b,c) on the original POJOs)
  • use the JAXB Context and the generated ObjectFactory class to marshall the JAXB-POJO object graph into an XML document

The original POJO model is a very simple one: the Company class has a name property and an employees property – which is a List<Employee>.

Image

Here is a snippet of the XML document that we will produce:

Image

This article uses JDeveloper 11gR2 for demonstrating the steps with JAXB – however, the steps will be similar and the code exactly the same in other IDEs.

Read the rest of this entry »

Create XSD from XML instance document – new JDeveloper 11gR1 feature

 

While looking into the XML functionality in JDeveloper 11g, I came across a feature that was added in the 11.1.1.1.0 release – July 2009: [Create] XML Schema from XML Document. Functionality previously found in commercial products such as XMLSpy, that enables us to make a head start with the development of XML Schema Definitions by using an existing XML Document as starting point.

In this article, I will very briefly demonstrate what this functionality allows us to do. And what its current limitations are.

In short: we can indicate an XML document and have an XSD created that derives its element, type and attribute definitions from the actual XML content in that document. Most XML documents do not represent the entire set of restrictions and freedom that the XSD will describe, so the generated XSD is only a starting point – but a very useful one all the same.

At this moment, the XML source document has to exist on the file system (we cannot feed the tool with a URL). The created XSD document does not work well with multiple namespaces – as well will see in this example. The tool does not create named (complexTypes) – only (nested) elements. It does create, when so requested, simpleTypes with enumerations that describe all occurring values in the source XML document. Such enumerations are usually required only for a limited number of elements. Of course removing the types we do not need is not a lot of work. Yet it would be comfortable to specify in more detail for which elements to create these enumerations.

Read the rest of this entry »

Book Review: Processing XML Documents with Oracle JDeveloper 11g by Deepak Vohra

 

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 be used from any IDE. However, as examples of using XDK 11g for tasks like parsing, validating, transforming and querying (XPath) it is very valuable and will save developers a lot of googling.

The book’s cover states "Inspired by the author’s previous XML articles for the Oracle community, this expanded hands-on tutorial guides newcomers and intermediate users through JDeveloper 11g and XML document development. "  Inspired seems putting it mildly: much of the book’s contents seems to consist of slightly refined versions of technical articles previously published on the Oracle Technology Network and other websites or books. Which is not certainly not necessarily a bad thing by the way. On the one hand it means that the chapters can be read in pretty much any order you like as they are fairly stand-alone. At the same time, the book does not have a clear overall thread or storyline except for a simple example – a simple and not very exciting Oracle Magazine article catalog document that makes appearances in most chapters.

Read the rest of this entry »

Publishing Java based WebServices using JAX-WS in JDeveloper 11g

Recently I have been doing quite some work with WebServices – calling them and implementing them, from and in both PL/SQL and Java. And in conjunction with BPEL and ESB. Today, I did some quick tests using JDeveloper 11g (TP4) and more specifically JAX-WS. It turns out to be very simple to publish (and test) a Java based WebService – a matter of minutes.

This article will very briefly demonstrate how to get a WebService up and running, based on a simple Java Class..... Read the rest of this entry »