[xsd-users] serialization into QDataStream binary format

Erik Sjölund erik.sjolund at gmail.com
Fri Feb 11 09:04:05 EST 2011


Hi,
Instead of serializing into XDR or CDR I tried serialize into QDataStream.
(for more info see http://doc.qt.nokia.com/4.7/qdatastream.html)

Here is the source code:
https://svn.sbc.su.se/repos/qdatastream-for-codesynthesis-xsd/tags/first-trial

I have only done one test and that was with the schema file
examples/cxx/tree/hello/hello.xsd
That test worked for me.

Here is some information of how I proceeded.

I started out by copying from the directory
xsd-3.3.0-x86_64-linux-gnu/libxsd/xsd/cxx/tree
the files
xdr-stream-insertion.hxx
xdr-stream-extraction.hxx
xdr-stream-common.hxx

to new file names;
qdatastream-stream-insertion.hxx
qdatastream-stream-extraction.hxx
qdatastream-stream-common.hxx

and adjusted them to QDataStream.

There is not much error handling. Maybe there are some exceptions from
Qt that could be caught to be able to throw the exceptions
qdatastream_stream_insertion
and
qdatastream_stream_extraction
?

Please also take a look at the functions for insertion and extraction of
 const buffer<C>& x
I saved the length into the datatype quint64. Is it the right choice?

In the file qdatastream-stream-extraction.hxx :
-------
int bytes_read = s.impl ().readRawData (x.data (), len_as_int);
if (bytes_read != len_as_int) {
    std::cerr << "readRawBytes did not read all bytes" << std::endl;
    exit (EXIT_FAILURE);
}
--------
If readRawData() reads less than requested we probably should not exit().
Maybe a while loop is better?

Feel free to modify the code and incorporate it into XSD. See my
modifications to these files as public domain.

To test it out you could do something like

xsdcxx  cxx-tree  --hxx-prologue '#include "qdatastream-stream-insertion.hxx"' \
                           --hxx-prologue '#include
"qdatastream-stream-extraction.hxx"' \
                           --generate-extraction QDataStream \
                           --generate-insertion  QDataStream  \
                           --generate-serialization

In your program you need to include these files:

#include <QDataStream>
#include <QFile>

and write something like:

QFile file("/tmp/qt.dat");
file.open(QIODevice::WriteOnly);
QDataStream q_datastream(&file);
xml_schema::ostream<QDataStream> ostream_qt(q_datastream);
ostream_qt << data_object;
file.close();

cheers,
Erik Sjölund



More information about the xsd-users mailing list