How to generate sample XML documents from their DTD or XSD?

XmlXsdDtdTest Data

Xml Problem Overview


We are developing an application that involves a substantial amount of XML transformations. We do not have any proper input test data per se, only DTD or XSD files. We'd like to generate our test data ourselves from these files. Is there an easy/free way to do that?

Edit

There are apparently no free tools for this, and I agree that OxygenXML is one of the best tools for this.

Xml Solutions


Solution 1 - Xml

In Visual Studio 2008 SP1 and later the XML Schema Explorer can create an XML document with some basic sample data:

  1. Open your XSD document
  2. Switch to XML Schema Explorer
  3. Right click the root node and choose "Generate Sample Xml"

Screenshot of the XML Schema Explorer

Solution 2 - Xml

In recent versions of the free and open source Eclipse IDE you can generate XML documents from DTD and XSD files. Right-click on a given *.dtd or *.xsd file and select "Generate -> XML File...". You can choose which root element to generate and whether optional attributes and elements should be generated.

Of course you can use Eclipse to create and edit your DTD and XSD schema files, too. And you don't need to install any plugins. It is included in the standard distribution.

Solution 3 - Xml

For Intellij Idea users:

Have a look at Tools -> XML Actions

enter image description here

Seems to work very well (as far as I have tested).

Edit:

As mentioned by @naXa, you can now also right-click on the XSD file and click "Generate XML Document from XSD Schema..."

Solution 4 - Xml

I think Oxygen (http://www.oxygenxml.com/) does it as well, but that's another commerical product. It's a nice one, though... I'd strongly recommend it for anyone doing a lot of XML work. It comes in a nice Eclipse plugin, too.

I do believe there is a free, fully-featured 30 day trial.

Solution 5 - Xml

The camprocessor available on Sourceforge.net will do xml test case generation for any XSD. There is a tutorial available to show you how to generate your own test examples - including using content hints to ensure realistic examples, not just random junk ones.

The tutorial is available here: http://www.oasis-open.org/committees/download.php/29661/XSD%20and%20jCAM%20tutorial.pdf

And more information on the tool - which is using the OASIS Content Assembly Mechanism (CAM) standard to refactor your XSD into a more XSLT friendly structure - can be found from the resource website - http://www.jcam.org.uk

Enjoy, DW

Solution 6 - Xml

XMLSpy does that for you, although that's not free...

I believe that Liquid Xml Studio does it for you and is free, but I have not personally used it to create test data.

Solution 7 - Xml

You can use the XML Instance Generator which is part of the Sun/Oracle Multi-Schema Validator.

It's README.txt states:

> Sun XML Generator is a Java tool to generate various XML instances from several kinds of schemas. It supports DTD, RELAX Namespace, RELAX Core, TREX, and a subset of W3C XML Schema Part 1. [...]

> This is a command-line tool that can generate both valid and invalid instances from schemas. It can be used for generating test cases for XML applications that need to conform to a particular schema.

Download and unpack xmlgen.zip from the msv download page and run the following command to get detailed usage instructions: > java -jar xmlgen.jar -help

The tool appears to be released under a BSD license; the source code is accessible from here

Solution 8 - Xml

Seems like nobody was able to answer the question so far :)

I use EclipseLink's MOXy to dynamically generate binding classes and then recursively go through the bound types. It is somewhat heavy, but it allows XPath value injection once the object tree is instantiated:

InputStream in = new FileInputStream(PATH_TO_XSD);
DynamicJAXBContext jaxbContext = 
            DynamicJAXBContextFactory.createContextFromXSD(in, null, Thread.currentThread().getContextClassLoader(), null);
DynamicType rootType = jaxbContext.getDynamicType(YOUR_ROOT_TYPE);
DynamicEntity root = rootType.newDynamicEntity();
traverseProps(jaxbContext, root, rootType, 0);

TraverseProps is pretty simple recursive method:

private void traverseProps(DynamicJAXBContext c, DynamicEntity e, DynamicType t, int level) throws DynamicException, InstantiationException, IllegalAccessException{
		if (t!=null) {
			logger.info(indent(level) + "type [" + t.getName() + "] of class [" + t.getClassName() + "] has " + t.getNumberOfProperties() + " props");
			for (String pName:t.getPropertiesNames()){
				Class<?> clazz = t.getPropertyType(pName);
				logger.info(indent(level) + "prop [" + pName + "] in type: " + clazz);
				//logger.info("prop [" + pName + "] in entity: " + e.get(pName));
				
				if (clazz==null){
					// need to create an instance of object
					String updatedClassName = pName.substring(0, 1).toUpperCase() + pName.substring(1);
					logger.info(indent(level) + "Creating new type instance for " + pName + " using following class name: " + updatedClassName );
					DynamicType child = c.getDynamicType("generated." + updatedClassName);
					DynamicEntity childEntity = child.newDynamicEntity();
					e.set(pName, childEntity);
					traverseProps(c, childEntity, child, level+1);
				} else {
					// just set empty value
					e.set(pName, clazz.newInstance());
				}
			}
		} else {
			logger.warn("type is null");
		}
	}

Converting everything to XML is pretty easy:

Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);

Solution 9 - Xml

You can also use XMLPad (free to use) found here http://www.wmhelp.com to generate your xml samples. From the menu : XSD -> generate sample XML file.

Solution 10 - Xml

Microsoft has published a "document generator" tool as a sample. This is an article that describes the architecture and operation of the sample app in some detail.

If you just want to run the sample generation tool, click here and install the MSI.

It's free. The source is available. Requires the .NET Framework to run. Works only with XSDs. (not Relax NG or DTD).

Solution 11 - Xml

XML-XIG: XML Instance Generator

http://xml-xig.sourceforge.net/

This opensource would be helpful.

Solution 12 - Xml

Microsoft Office has 'InfoPath', which takes an XSD as an import and lets you quickly and easily define a form-based editor for creating XML files. It has two modes - one where you define the form, and another mode where you create the XML file by filling out the form. I believe it first came with Office 2003, and most people never install it. It shocks me at how much I like it.

Solution 13 - Xml

XMLBlueprint 7.5 can do the following:

  • generate sample xml from dtd
  • generate sample xml from relax ng schema
  • generate sample xml from xml schema

Solution 14 - Xml

Liquid XML Studio has an XML Sample Generator wizard which will build sample XML files from an XML Schema. The resulting data seems to comply with the schema (it just can't generate data for regex patterns).

Generate an XML Sample from an XSD

Solution 15 - Xml

The open source Version of SoapUI can generate SOAP requests from WSDL (which contains XSD type definitions), so it looks like there IS an open source implementation of this functionality. Unfortunately, I haven't figured out which library is used to to this.

Solution 16 - Xml

The OpenXSD library mentions that they have support for generating XML instances based on the XSD. Check that out.

Solution 17 - Xml

For completeness I'll add http://code.google.com/p/jlibs/wiki/XSInstance, which was mentioned in a similar (but Java-specific) question: https://stackoverflow.com/q/4710852/223837

Solution 18 - Xml

XML Blueprint also does that; instructions here

http://www.xmlblueprint.com/help/html/topic_170.htm

It's not free, but there's a 10-day free trial; it seems fast and efficient; unfortunately it's Windows only.

Solution 19 - Xml

There's also http://xsd2xml.com/, an online XSD to XML generator

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlindelofView Question on Stackoverflow
Solution 1 - XmlSam WarwickView Answer on Stackoverflow
Solution 2 - XmlFlorian FankhauserView Answer on Stackoverflow
Solution 3 - XmlyamassView Answer on Stackoverflow
Solution 4 - XmllevandView Answer on Stackoverflow
Solution 5 - XmlDavid WebberView Answer on Stackoverflow
Solution 6 - XmlSCdFView Answer on Stackoverflow
Solution 7 - XmlfalkoView Answer on Stackoverflow
Solution 8 - XmlMichal RamesView Answer on Stackoverflow
Solution 9 - XmlPat BView Answer on Stackoverflow
Solution 10 - XmlKobusView Answer on Stackoverflow
Solution 11 - XmlrolandpengView Answer on Stackoverflow
Solution 12 - XmlKieveliView Answer on Stackoverflow
Solution 13 - XmlPeterView Answer on Stackoverflow
Solution 14 - XmlSprottyView Answer on Stackoverflow
Solution 15 - Xmlotto.poellathView Answer on Stackoverflow
Solution 16 - XmlAravind YarramView Answer on Stackoverflow
Solution 17 - XmlMarnixKlooster ReinstateMonicaView Answer on Stackoverflow
Solution 18 - XmlBambaxView Answer on Stackoverflow
Solution 19 - XmlSphinxxxView Answer on Stackoverflow