Have you ever received an XML Sample without a Schema that you wanted to use with JAXB or some other XML Binding? It actually happens more than I would like. The common thing to do is simply not to use any XML Binding at all. This is not ideal since JAXB is so much easier to use than DOM. Yes there are better Java libraries out there to deal with XML (or Groovy) but that is not the topic of discussion here.
You can always try to write your own schema from the sample provided but that can take some time to do. Many times by the time you are done, you could have just used something else. Another option is to generate a schema for you.
Let us consider the following scenario. You have been provided the following XML sample.
The first thing you need to do to use JAXB is to get a schema for this XML. There is a free software tool called the Trang Converter that can be used to convert between schema types. It has a cool feature that can generate a schema from an XML sample file. This is what I am going to do here.
The XML editor I use is called Oxygen XML. It actually has the Trang Converter built in as an option. Going to Tools->Schema Converter gives you a UI on top of the Trang Converter.
Using this, you take the sample and generate a schema for it. After the convert, you end up with a schema looking something like this:
You can then use JAXB to generate your object model from this using the following command:
The result looks like this:
Now you have a JAXB generated object model from just an XML sample file.
Example of the generate Person.java class.