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>
Create classes using xsd tool:
xsd.exe /classes Test.xsd
This will generate Test.cs file.
Add Test.cs file to your solution.
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
Should Function Declarations Include Parameter Names
Compiling Multithread Code with G++ (-Wl,-No-As-Needed Not Working)
Is Rdtsc Timer Inaccurate in Linux
Opencv 2.4.2 Calcopticalflowpyrlk Doesn't Find Any Points
Using Crypto++ Static Library in a Qt Project
Has a Std::Byte Pointer the Same Aliasing Implications as Char*
Some Issues About Cygwin[Linux in Windows] (Socket,Thread,Other Programming and Shell Issues)
Struct Member Alignment - How to Assume No Padding
How to Check If a File Is Still Being Written
How to Query Vsync Phase in Linux
Why Is My Class Non Default-Constructible
Getting Errors While Compiling
Sdl Fake Fullscreen Mode on Dual Monitor Setup Under Linux
Get Functions Names in a Shared Library Programmatically
Access Processor Interrupts with C++ and X86 and X64 Architectures
How to Set Background Color of Window After I Have Registered It
Popen() Writes Output of Command Executed to Cout
How to Stop Windows from Blocking the Program During a Window Drag or Menu Button Being Held Down