Posts tagged rss

Turning any XML document into a Java Object graph using JAXB 2.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).

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 »