Tool to Generate Xml File from Xsd (For Testing)

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

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.

Any Java API to generate Sample XML from XSD?

There is no such API, but it's possible.
'gives out sample XML' means that you will have to implement generation of sample XML node(s) from XSD basic types like <xs:element name="value" type="xs:integer" minOccurs="0"/>, taking care of minoccurs/maxoccurs attributes, not storing dates in xs:integer nodes, etc, etc..

Once it's done, the rest is not a problem: traversing XSD with XPath or org.w3c.dom.*, flattening complex types and extensions will do the trick. I bet you'll receive working traversing code here on stackoverflow within minutes after posting question.

Any tools to generate an XSD schema from an XML instance document?

the Microsoft XSD inference tool is a good, free solution. Many XML editing tools, such as XmlSpy (mentioned by @Garth Gilmour) or OxygenXML Editor also have that feature. They're rather expensive, though. BizTalk Server also has an XSD inferring tool as well.

edit: I just discovered the .net XmlSchemaInference class, so if you're using .net you should consider that

Is there a XSD-driven random XML test data generator?

Many tools exist to do this, check theses links

  • Microsoft XmlSampleGenerator (free), you can download the source code and build with visual studio.
  • Liquid XML Sample Generator (Great tool)
  • XMLSpy from Altova (Expensive but complete)
  • XML Schema Editor from Oxygen
  • XML Schema Explorer, included with visual studio
  • XML Generator from Stylus studio

Generate XML document matching XSD

Microsoft has something called the XmlSampleGenerator. Check this out:

http://msdn.microsoft.com/en-us/library/aa302296.aspx

here are it's limitations:

  • The W3C XML Schema Identity Constraints (xs:key, xs:keyref,
    xs:unique) are not supported while generating an instance document.
  • If xs:pattern facets exist on simple types, values generated may not
    conform to the pattern.
  • Enumerations of the xs:QName type may not
    work as expected since this requires the prefixes in the schema to be
    preserved.
  • xs:ENTITY, xs:ENTITIES, and xs:NOTATION types are not
    supported.
  • xs:base64Binary content is generated only if enumerations
    exist in the schema for that type.

Generating XML file using XSD file

Suppose we have Test.xsd file that looks like this:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MyClass">
<xs:complexType>
<xs:sequence>
<xs:element name="Field1"
type="xs:string"/>
<xs:element name="Field2"
type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
  1. Create classes using xsd tool:

    xsd.exe /classes Test.xsd

    This will generate Test.cs file.

  2. Add Test.cs file to your solution.

  3. Create instance of MyClass, defined in XSD schema and XmlSerialize it:

    using System.Xml.Serialization;
    // ...
    var data = new MyClass { Field1 = "test1", Field2 = "test2" };
    var serializer = new XmlSerializer(typeof(MyClass));
    using (var stream = new StreamWriter("C:\\test.xml"))
    serializer.Serialize(stream, data);

Result:

<?xml version="1.0" encoding="utf-8"?>
<MyClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Field1>test1</Field1>
<Field2>test2</Field2>
</MyClass>


Related Topics



Leave a reply



Submit