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

258,490

Solution 1

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 2

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 3

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 4

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 5

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

Share:
258,490

Related videos on Youtube

lindelof
Author by

lindelof

David's dayjob consists of working as a consultant for an IT company in Geneva. After dark he works on his main interests, which include home and building automation.

Updated on September 11, 2020

Comments

  • lindelof
    lindelof over 3 years

    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.

    • mjn
      mjn almost 11 years
      I fail to see answers for this questions which do not fit the Q&A format, or solicit debate, arguments, polling, or extended discussion
  • Admin
    Admin over 15 years
    Direct link to Sourceforge.net download is: sourceforge.net/projects/camprocessor
  • Hans-Peter Störr
    Hans-Peter Störr over 15 years
    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.
  • lindelof
    lindelof over 15 years
    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!
  • balint
    balint almost 15 years
    yes, this is the easiest way. Open XSD, switch to XML Schema Explorer, select the root node, right click and choose "Generate Sample Xml".
  • Rachel
    Rachel over 14 years
    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 ?
  • Akku
    Akku over 13 years
    Thanks! Eclipse is free, and this was the perfect solution.
  • Sean Kearon
    Sean Kearon about 13 years
    The installer drops a bunch of C# code onto your machine. Just dump those into LinqPad and you have your free XML instance generator. Ta dah!
  • otto.poellath
    otto.poellath about 13 years
    Do you guys happen to know what Eclipse feature provides this functionality? My "Generate" submenu has "JAXB Classes" as the only item.
  • pwaring
    pwaring almost 13 years
    You need to install the Web Tools Project (look under Help->Install New Software) to get these options.
  • Eric H
    Eric H over 12 years
    Thanks leveland. This answered my question as well! If you would like to come answer my question: stackoverflow.com/questions/7041388/…, I would be more than happy to mark yours as the answer.
  • David Hergert
    David Hergert over 12 years
    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?
  • Jarekczek
    Jarekczek over 11 years
    Current links: homepage, downloads. xmlgen.zip only in nigthly directory. I confirm inability to generate a sample from my xsd.
  • Jarekczek
    Jarekczek over 11 years
    Not very user friendly. Throws a lot of exceptions, can't make it working after getting unexplained NullPointerException.
  • Simon
    Simon about 11 years
    I used this with VS2008, but I can't find this in VS2010. Can anybody confirm that this still exists?
  • Martijn
    Martijn about 11 years
    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... :-(
  • Nikita
    Nikita about 11 years
    is there a way to specify # of elements to generate (for sequences)?
  • Miguel Sevilla
    Miguel Sevilla about 11 years
    @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).
  • Adam Matan
    Adam Matan almost 11 years
    @DavidHergert A simple workaround would be generating a stub type.
  • Adam Matan
    Adam Matan almost 11 years
    Florian - Thanks. This just saved me 4 hours of xmlbeans guess-compile-fix iterations.
  • Steve Chambers
    Steve Chambers almost 11 years
    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.
  • Christian Vielma
    Christian Vielma almost 11 years
    I think Eclipse is the best!
  • FLX
    FLX over 9 years
    I tested every solution of this thread and this worked the best
  • Envek
    Envek over 9 years
    Does not work for me in latest RubyMine (v. 7.0, Intellij Idea based) with XSD with many imports. I've got nothing but error message.
  • stefan
    stefan almost 9 years
    XML Schema Explorer is not present in Visual Studio 2013 for Web neither. VS2013 Community does have it.
  • osullivj
    osullivj about 8 years
    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.
  • Eliot
    Eliot about 8 years
    Not available in Visual Studio Code (as of early 2016), but there in Visual Studio 2015 Community on Windows.
  • The Red Pea
    The Red Pea about 8 years
    I get a stack overflow or "WMHelp XmlPad has stopped working" when I choose "Generate Sample Data"
  • The Red Pea
    The Red Pea about 8 years
    "Schema Compilation Error"?
  • The Red Pea
    The Red Pea about 8 years
    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...
  • Matt
    Matt almost 8 years
    Unfortunately where there's an xsd:choice in the xsd it only picks one, so it's a pretty anaemic example.
  • Matt
    Matt almost 8 years
    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
  • Ghoti
    Ghoti about 7 years
    @Pyderman - that's your settings - look up how to change them.
  • Davos
    Davos about 7 years
    @TimAbell , if it's xsd:choice then an example xml file can only contain one of that choice. msdn.microsoft.com/en-us/library/ms256109(v=vs.110).aspx
  • Rok T.
    Rok T. over 6 years
    Visual Studio Community 2017 still has this feature.
  • RedGrittyBrick
    RedGrittyBrick over 6 years
    java.net links are basically dead.
  • naXa stands with Ukraine
    naXa stands with Ukraine over 6 years
    In newer versions, right-click on XSD and select "Generate XML Document from XSD Schema..."
  • naXa stands with Ukraine
    naXa stands with Ukraine over 6 years
    can you please post a usage example?
  • Czar
    Czar over 5 years
    Constantly says "Root element name is invalid" and doesn't do anything, "Ok" button inactive.
  • Mikanikal
    Mikanikal over 5 years
    9 years later, this is still working great for XSDs.
  • MeanGreen
    MeanGreen over 5 years
    @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...
  • ka3ak
    ka3ak about 3 years
    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 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
    ka3ak about 3 years
    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
  • sshaw
    sshaw about 2 years
    I have tried many options for this and have found Oxygen 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 and it worked great. The others couldn't do it.
  • Simon Logic
    Simon Logic almost 2 years
    "Generate XML Document from XSD Schema..." does not create really full xml actually. At least for 2021.3