I know there have been several threads about generating xml from XSD schema, but seems nobody actually has a need to do it programmatically. I feel I actually have a valid use for it.
I am writing software that interfaces with an external application that sends xml data to load into a form (a PDF). Our service returns the PDF with the loaded xml data, and the user can then edit and submit the form to us.
When the form is submitted to us, we have a process that extracts the xml data in the original load format, and we convert the data to an alternate 'submit' xml format.
A form can also be reloaded with submitted data, in which case we get the submit xml format and convert it back into the original load format, load it into the form and return it back again.
To do this, we maintain a 'skeleton' xml structure of both load and submit formats, and XSLT is used to translate between these formats.
This is working well for us - the skeleton xml allows us to do a copy transform of the destination format, and fill in the missing parts from the other source format. One advantage we have is all non-leaf elements correspond to object names from the external application, and we have a mapping between the names, so translating isn't too complex.
However, the skeleton xml are maintained manually, and updating it for data changes is cumbersome.
The next thing we want to do is provide a way to accept an xsd schema for both the load and submit document formats, and generate the skeleton xml automatically.
The documents defined by these schemas have no namespace. They are authored by other developers, not in an automated way.
There is no mixed content allowed. That is, elements can contain elements only, or text only.
The rules for this sample xml are:
elements that can contain only text content should not be created in the sample xmlall other optional and mandatory elements should be included in the sample xmlelements should be created only once even if they can occur multiple timesany other nodes such as attributes, comments, processing instruction, etc. should be ommited - the sample xml would be an 'element tree'
Are there APIs or tools in
Java that can generate such sample xml? I'm looking for pointers where to get started.
This needs to be done programmatically in a reliable way, as stated earlier, the sample xml is used by other XSLT transformations.