202

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.

lindelof
  • 34,556
  • 31
  • 99
  • 140
  • 8
    I fail to see answers for this questions which do not fit the Q&A format, or solicit `debate, arguments, polling, or extended discussion` – mjn May 02 '13 at 11:47

19 Answers19

184

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

Justin
  • 84,773
  • 49
  • 224
  • 367
Sam Warwick
  • 2,822
  • 3
  • 19
  • 17
  • 15
    yes, this is the easiest way. Open XSD, switch to XML Schema Explorer, select the root node, right click and choose "Generate Sample Xml". – balint Jul 18 '09 at 16:58
  • 3
    I used this with VS2008, but I can't find this in VS2010. Can anybody confirm that this still exists? – Simon Feb 01 '13 at 07:27
  • Same as Simon: I can’t find this in VS2010 either. According to MSDN, it should still be there, but it just won't show up anywhere in the UI... :-( – Martijn Feb 08 '13 at 15:20
  • 6
    @Simon: you only get the "Generate Sample XML" when you right click one of the elements in the schema explorer. Elements are depicted graphically via the green left/right angle brackets: "<>". You are probably right clicking a type, and the generate message will only generate it for elements (which makes sense). – Miguel Sevilla Apr 08 '13 at 16:53
  • XML Schema Explorer is not present in Visual Studio 2013 for Web neither. VS2013 Community does have it. – stefan Jun 17 '15 at 12:08
  • Note that you do need VS 2008 SP1 for this to work. I was running on a vanilla VS 2008, and XML Schema Overview did not appear on my View menu. After applying SP1 I was able to open a .xsd and have the Schema view appear automatically. – osullivj Jan 27 '16 at 09:36
  • Not available in Visual Studio Code (as of early 2016), but there in Visual Studio 2015 Community on Windows. – Eliot Mar 09 '16 at 11:48
  • Unfortunately where there's an `xsd:choice` in the xsd it only picks one, so it's a pretty anaemic example. – Tim Abell Jul 07 '16 at 09:05
  • 1
    @TimAbell , if it's `xsd:choice` then an example xml file _can only_ contain one of that choice. https://msdn.microsoft.com/en-us/library/ms256109(v=vs.110).aspx – Davos Apr 28 '17 at 01:16
  • Visual Studio Community 2017 still has this feature. – Rok T. Aug 25 '17 at 07:05
  • @RokT. it does, but it's not able to generate a valid XML for me. The generated file immediately has a warning. The value -99 is obviously not valid for a NonNegativeInteger... – MeanGreen Jan 10 '19 at 10:26
131

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.

Florian Fankhauser
  • 3,615
  • 2
  • 26
  • 30
  • Thanks! Eclipse is free, and this was the perfect solution. – Akku Jan 26 '11 at 13:15
  • Do you guys happen to know what Eclipse feature provides this functionality? My "Generate" submenu has "JAXB Classes" as the only item. – otto.poellath Mar 10 '11 at 16:27
  • 9
    You need to install the Web Tools Project (look under Help->Install New Software) to get these options. – pwaring May 13 '11 at 15:54
  • 3
    Eclipse seems like a good option, but I could not get the generation to follow imported XSDs. For example, if I had an XSD describing 3 elements, and one of those was a complexType that was defined in a referenced/imported XSD, it would not generate for that element. Any ideas? – David Hergert Dec 16 '11 at 15:55
  • @DavidHergert A simple workaround would be generating a stub type. – Adam Matan Apr 30 '13 at 11:19
  • Florian - Thanks. This just saved me 4 hours of xmlbeans guess-compile-fix iterations. – Adam Matan Apr 30 '13 at 11:20
  • I prefer this to the higher rated Visual Studio method as it has more options. (Visual Studio generated me a very scant XML file with anything nillable set to nil, whereas Eclipse gave me the option to populate these elements.) One minor q - does the XSD file have to be plonked somewhere in the workspace? I was hoping to be able to do it via Open File... for any file on my computer or network without copying it into the workspace but couldn't find a way. – Steve Chambers May 01 '13 at 11:49
  • I think Eclipse is the best! – Christian Vielma May 01 '13 at 19:08
42

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..."

yamass
  • 765
  • 5
  • 14
30

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.

levand
  • 8,440
  • 3
  • 41
  • 54
  • I have an bunch of XSDs which in turn inherits schema's from different XSD's and I need to generate an XML file for testing purpose from this XSD, am using oxygenXML to do this but when I try to do it, it is not working for me and it says please define root node but than it does not allow me to do so. So if anyone had worked with oxygenXML do suggest of what could be way around for this ? – Rachel Oct 21 '09 at 21:41
  • Thanks leveland. This answered my question as well! If you would like to come answer my question: http://stackoverflow.com/questions/7041388/xmlvend-example-xml-request-and-response, I would be more than happy to mark yours as the answer. – Eric H Aug 12 '11 at 14:50
  • I have tried many options for this and have found [Oxygen](https://www.oxygenxml.com/) to be the best. It is commercial but does offer a free trial. I have used this to generate XMLs from the most bloated schema ever: [DDEX ERN](https://github.com/sshaw/ddex/blob/ea976d60c5f72e0fea0f5f3239a2125509bd3dbd/etc/schemas/ern/42/release-notification.xsd) and it worked great. The others couldn't do it. – sshaw Feb 05 '22 at 17:32
12

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

10

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.

karlingen
  • 13,800
  • 5
  • 43
  • 74
SCdF
  • 57,260
  • 24
  • 77
  • 113
  • It generates something, but you don't have much control over it: you can choose the toplevel element and some general options, but you cannot use XMLSpy to generate set of testcases with many variants etc. – Dr. Hans-Peter Störr Dec 23 '08 at 10:05
  • XMLSpy looked good but generated xml that then failed to validate against the xsd that was used to generate it. Fail. It also barfed on a `postiveinteger` field - putting `*ERROR*` in instead – Tim Abell Jul 07 '16 at 12:56
10

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

m01
  • 9,033
  • 6
  • 32
  • 58
falko
  • 101
  • 1
  • 2
  • Very nice! A little bit limited though. I tried it on an XSD that imports other XSD and it failed. But on a DTD it works very well. Thanks! – lindelof Jan 15 '09 at 11:00
  • 1
    Current links: [homepage](http://msv.java.net/), [downloads](http://java.net/downloads/msv/). `xmlgen.zip` only in `nigthly` directory. I confirm inability to generate a sample from my xsd. – Jarekczek Sep 07 '12 at 11:55
  • In my case, needed to use the `--example` file to contain tokens that can be used to generate a new sample file. It takes a sample to make a sample! Not easy... – Nate Anderson Mar 23 '16 at 06:38
  • java.net links are basically dead. – RedGrittyBrick Oct 17 '17 at 10:10
7

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.

Pat B
  • 1,915
  • 23
  • 40
7

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);
Michal Rames
  • 71
  • 1
  • 1
5

XML-XIG: XML Instance Generator

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

This opensource would be helpful.

rolandpeng
  • 51
  • 1
  • 2
  • 1
    Not very user friendly. Throws a lot of exceptions, can't make it working after getting unexplained `NullPointerException`. – Jarekczek Sep 07 '12 at 12:11
5

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).

Cheeso
  • 189,189
  • 101
  • 473
  • 713
4

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.

Kieveli
  • 10,944
  • 6
  • 56
  • 81
1

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

Peter
  • 11
  • 1
1

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

Sprotty
  • 5,676
  • 3
  • 33
  • 52
1

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.

otto.poellath
  • 4,129
  • 6
  • 45
  • 60
0

For completeness I'll add http://code.google.com/p/jlibs/wiki/XSInstance, which was mentioned in a similar (but Java-specific) question: Any Java "API" to generate Sample XML from XSD?

Community
  • 1
  • 1
  • I tried this and ran it as suggested: `xsd-instance.sh purchase-order.xsd {http://jlibs.org}PurchaseOrder` Got an error: `Error: Unable to initialize main class jlibs.xml.xsd.XSInstance Caused by: java.lang.NoClassDefFoundError: org/apache/xerces/xs/XSObject` – ka3ak Feb 06 '21 at 11:51
0

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.

Bambax
  • 2,920
  • 6
  • 34
  • 43
0

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

Sphinxxx
  • 12,484
  • 4
  • 54
  • 84
  • It didn't generate a valid XML from an XSD unfortunately. However the other tools failed at this task - Eclipse, Intellij IDEA and even XMLSpy. The best I've found so far is https://www.liquid-technologies.com/online-xsd-to-xml-converter But even this one fails to generate a valid XML if there are patterns in XSD. – ka3ak Feb 06 '21 at 11:47
0

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

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327