metdos
metdos

Reputation: 13929

Generating XML Documents from XML Schemas in C++

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.

Upvotes: 9

Views: 2824

Answers (2)

jordenysp
jordenysp

Reputation: 2794

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";
    }

Upvotes: -1

xcut
xcut

Reputation: 6349

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.

Upvotes: 4

Related Questions