views:

244

answers:

5

Is there any easy way to create at least a template XML file using XML Schema? My main interest is bounded by C++, but discussions of other programming languages are also welcome.By the way I also use QT framework.

+1  A: 

Check this one... http://stackoverflow.com/questions/17106/how-to-generate-sample-xml-documents-from-their-dtd-or-xsd.. It has accepted answer too and lot other suggestions..

liaK
+2  A: 

You may have to write this yourself. There is no one way of getting an XML file from a Schema. If you can make domain-specific assumptions (e.g. how to populate data items, which items to choose in case of a choice, how often to insert domain-specific elements) then you will get a better instance document.

If you are working with C++, note that Xerces C++ allows you to load a schema and access its model (i.e. access it properly, not just load the schema as an XML document). I suggest you download it an check out the SCMPrint Sample. It will show you how to traverse a schema. You can then modify that to print out a sample XML file instead.

xcut
A: 

In Qt 4.5 the XML support has been extended by an XSLT implementation, that allows you to easily convert documents from one XML dialect into another or generating source code from a XML description

W3C XML Schema validation with Qt

Example:

    #include <QtXmlPatterns/QXmlSchema>
    #include <QtXmlPatterns/QXmlSchemaValidator>

    QXmlSchema schema;
    schema.load( QUrl("file:///home/jordenysp/example.xsd") );

    if ( schema.isValid() ) {
        QXmlSchemaValidator validator( schema );
        if ( validator.validate( QUrl("file:///home/jordenysp/result.xml") ) ) {
            qDebug() < < "Is valid";
        } else {
            qDebug() << "Is invalid";
        }
    } else {
        qDebug() << "Is invalid";
    }
jordenysp
Your example is a validation example, not a transformation of XML.
metdos
A: 

You should try free XmlPlus xsd2cpp tool, for XML Schema to C++ Data Binding.
XmlPlus xsd2cpp

The xsd2cpp binary when invoked on an [input].xsd, generates:
* the C++ sources(implementation/headers) for the supplied XML-Schema
* a main.cpp template, to demonstrate how generated sources can be consumed
* the automake/autoconf files for building the generated source

The main.cpp file is built into a [input]run binary for an [input].xsd input, which can be used to:
- generate sample xml documents, without any additional C++ code
- validate an instance xml document against the Schema: [input].xsd
- generate populated instance xml document, if you write code in the template function inside main.cpp to populate the Document.
- option to roundtrip ie. read and instance xml file into C++ structures, and then write back to xml

A brief note on XmlPlus xsd2cpp:
* provides C++ Data-Binding, validating-parser, writer for xml-files constrained by the supplied XML-schema * roundtrip retains comments and processing instructions
* contains good examples to demonstrate usage
* tested against some of randomly selected W3C test cases

Satya Prakash Tripathi