1

I have a xsd that in turn uses/imports a set of xsds. I would like to programtically generate sample xml from the xsd. The xml must contain all elements and attributes populated with example data based on data type.

How can I do this using eclipse api classes? Also are there any other tools that accomplish this task and can be evoked in a java program or a batch file?

Any pointers to examples/documentation/api is highly appreciated.

Thanks in advance.

Chadwick
  • 12,555
  • 7
  • 49
  • 66
joe
  • 11
  • 1
  • 3
  • Hi Joe, welcome to SO! Just a friendly note that there's no need to "tag" questions by putting leading keywords in the title, tags do that for us. – Chadwick Jan 06 '11 at 19:34
  • possible duplicate of [java/xsd parsing](http://stackoverflow.com/questions/4484610/java-xsd-parsing) – Aravind Yarram Jan 06 '11 at 20:18

1 Answers1

1

if I am reading your question correctly, I believe what you are trying to do is programmatically generate (i.e. using Java) XML documents based on an XML Schema Document (which may in turn import other supporting XSD's).

You may wish to have a look at Oracle/Sun's JAXB (Java Architecture for Xml Binding) which you can find more info about here:

http://jaxb.java.net/

JAXB works with J2SE-SDK and/or IDEs - such as Netbeans or Eclipse, and permits you to unmarshall (read XML documents into mapped Java Objects) or marshall (write Java objects as XML documents) as required. Standard mappings (known as binding declarations) are provided based on valid XML Schema provided to JAXB. You can also provide binding declarations through custom annotations directly within your XML Schema files or by using external JAXB declarations.

Another alternative (similar to JAXB) is Apache's XML-Beans.

Hope this helps!

gto406
  • 589
  • 3
  • 9
  • We decided not to use JAXB in order to avoid geenration of java classes and lean towards usage eclipse API usage. In that effort I found useful information from Rich Seller at http://stackoverflow.com/questions/1497064/headless-xml-generation-from-xsd-with-eclipse I tried out that example and it generatges xml with empty content for the elements. Thus I am now trying to enhance the code to generate xml with sample data for ALL elements/attributes (required/optional) as per the data type specified in the xsd. I do not find the right link for eclipse claases documentation to achieve this. – joe Jan 06 '11 at 22:49
  • Hi Joe, you are absolutely correct, not easy to find Javadoc for this! I had to hunt around for the source-code. It seems (as RichS notes) you are running the UI element wizard 'headless' (i.e. without the UI). I believe your 'only option' which I think you have already figured out, is to expand the XSD as much as possible so that the 'generation' of the XML works from your ContentModel. E.g. by specifying 'default' values for attributes you may get those in the generated file. Best of luck with this! – gto406 Jan 07 '11 at 20:55