PDA

View Full Version : Serialization Of Xml using QObjects



StarRocks
2nd January 2013, 06:18
Dear Forums,


I have a doubt regarding using XML serialization to generically serialize any ole QObject. This kinda perplexes me because all the needed elements appear to be within Qt. Qt has XML serialization classes and reflection through its meta data system, yet no XML serialization function that accepts a QObject.Please provide me with a specific solution...........Thanks in Advance......Any solution would be appreciable...............


Regards,

Santosh Reddy
2nd January 2013, 07:32
In general XML is used for information exchange, where you would typically serialize somthing (QObject) to send it across to some other party (or software etc). Now the important requirement is that both parties should agree on the XML schema of the serialized QObject. I think it is this lack of a standard of specifying the schema of the QObject is one main reason for not having such serialization API/function.

Moreover it gets complicated for custom (derived QObjects) where the derivered QObject definition may not be always be inline with Qt Meta System. (I mean properties etc), may be this is another reason.

Anyway it is possible to come up with a simple mechanism using a combination of one or more of the framework classes QDocDocument, QSetting, QVariant, QXmlStreamWriter, to write a custom XML serialization schema

StarRocks
2nd January 2013, 08:53
Dear Santosh,


Thanks for the reply........Actually i dint understand the way you said ..........Can you give me an example so that it would be very favourable to understand for me..........Any solution would be appreciable.........Thanks in Advance........


Regards,

Santosh Reddy
2nd January 2013, 09:03
I meant that Qt already has enough support so that one can write serialization (XML or any other type) them selfs

StarRocks
2nd January 2013, 10:02
Dear Santosh,


Thanks for the reply..........Can you just me a sample program on how to do it pretty confused in writing it..............Any solution would be appreciable........Thanks in Advance........


Regards,

Santosh Reddy
2nd January 2013, 10:03
It depends, what do you want to do with the serialized XML data?

StarRocks
2nd January 2013, 11:33
Dear Santosh,

Thanks for the reply..........What i need is i have to hit the live database of our .net application so for that i need to serialize my data in the form of an object and then send it to hit the live db of our application...........Hope you got me..........Any solution would be appreciable.Thanks in advance........


Regards,

Santosh Reddy
2nd January 2013, 11:38
It is not clear to me.

Who will read the DB (the Qt Applciation / .Net serialization API)?

StarRocks
2nd January 2013, 11:42
Dear Santosh,

I will explain in brief i have a device,In that Device database i save my transaction data ..I need to insert this data in server DB....so now i get my required data from Device db to transfer to server through QUERYING. For example i got 5 rows with 10 columns........now to insert this records in SERVER Database,I need to do serialization for these result and hit the web service with that serialized object to pass these data and get inserted in the SERVER Database....????
->Passing anything either xml/json serialization object has to been done, to hit web service .Hope you got me now...........Thanks in Advance.........Any Solution would be appreciable........


Regards,

Santosh Reddy
2nd January 2013, 12:02
Who do you think will read/get this serilaized data from server, and how will they understand the xml serialization scheme you used to serialize?

I think i am completly off the page, or missed someting.

StarRocks
2nd January 2013, 12:50
Dear Santosh,

Thanks for the reply.........Actually we have an Service(Api) in the .net now what we are going to do is we serialize(Xml/Json) the data that we get from the Qt as an object say for example the object contains about more than 10 fields and send that to the server after serialization(Xml/Json)......In the Server through this Service it gets hit ....Hope you got me...........Thanks in Advance.......Any Solution would be appreciable....

Regards,

Santosh Reddy
2nd January 2013, 13:10
I am not sure of Service(Api), whatever it is, find out the XML scema it requires and useQDomDocument (http://qt-project.org/doc/qt-4.8/qdomdocument.html) to prepare the XML data. Refer Qt docs for QDomDocument (http://qt-project.org/doc/qt-4.8/qdomdocument.html), it shows how to creare XML.

wysota
2nd January 2013, 13:42
I think OP's app is going to be the only one reading the data (the DB is probably only for storage) so it doesn't really matter how the serialization is implemented as long as it is reversible. The most trivial (in terms of implementation) solution I can think of is to store the class name and values for all properties of a given object as separate tags in the xml (using QObject meta-data) before proceeding to children of the object. It should be easy to implement using QXmlStreamWriter and QXmlStreamReader.

anda_skoa
2nd January 2013, 14:59
Lets assume we have a database query and we want to format it in XML as a table. That would be something like this:



QString xmlText;
QXmlStreamWriter writer( &xmlText );

writer.writeStartDocument( "1.0" );
writer.writeStartElement( "table" );

while ( query.next() ) {
QSqlRecord row = query.record();

writer.writeStartElement( "row" );

for ( int col = 0; col < row.count(); ++col ) {
QSqlField column = row.field( col );

writer.writeStartElement( "column" );

writer.writeAttribute( "name", field.name() );
writer.writeCharacters( field.value().toString() );

writer.writeEndElement(); // column
}

writer.writeEndElement(); // row
}

writer.writeEndElement(); // table
writer.writeEndDocument();


Cheers,
_

StarRocks
3rd January 2013, 07:18
Dear anda_skoa,


Thanks for the reply...........It would be very favourable for all of us if you provide us with a better examples....Because the one you provided is not that clear to me as im pretty new to this concept too,hope the better example that you give will help me as well as others who are looking for it too....Any Solution would be appreciable.....Thanks in Advance......


Regards,

wysota
3rd January 2013, 09:21
What exactly of what anda_skoa provided is not clear to you? The code is pretty much self explanatory.

StarRocks
3rd January 2013, 09:26
Dear wysota,


Thanks for the comments........Actually im very new to this concept so what he has given the code might be very clear by as im a beginner in this not getting what exactly he has done....If i am little bit familiar with it then i can code the remaining,but my problem is im pretty new and not knowing how to code a sample QDomDocument it would be favourable if you give me an idea with an example.....Thanks in Advance....Any solution would be appreciable......



Regards,

wysota
3rd January 2013, 10:48
Thanks for the comments........Actually im very new to this concept so what he has given the code might be very clear by as im a beginner in this not getting what exactly he has done....If i am little bit familiar with it then i can code the remaining,but my problem is im pretty new and not knowing how to code a sample QDomDocument it would be favourable if you give me an idea with an example.....Thanks in Advance....Any solution would be appreciable......

Repeating over and over that you are new to a subject or that you're not familiar with it will not get you anywhere. Read the docs and examples that go with it about QDomDocument or QXmlStreamWriter and then come back here and ask specific questions about specific things you don't understand. If you're expecting to get code that you can simply copy and paste into your application that will do exactly what you want, then you will be disappointed -- nobody here will provide you with such code. There is no QSerializeQObjectsIntoXmlTheWayStarRocksNeedsIt class in Qt.

StarRocks
3rd January 2013, 11:39
Dear wysota,


Thanks for the comments..........I have done a code but i am getting an error message as "error: expected type-specifier before 'frmMain'" ....I guess the frmMain should be as the Class name as Contact but when i give that its not showing me an option "show"...i guess im missing something....please find the attachment of my code for your reference......Any solution would be appreciable......Thanks in Advance......



Regards

wysota
3rd January 2013, 12:15
You forgot to include the file that defines the "frmMain" class.

StarRocks
3rd January 2013, 12:20
Dear Wysota,


Thanks for the reply.......But actually i am not knowing where this "frmMain" class is coming from because the name of my class is Contact right then what exactly frmMain is.....Is that a builtin file or library .........Can you help out me here......Any solution would be appreciable.....Thanks in advance............



Regards,

wysota
3rd January 2013, 12:29
But actually i am not knowing where this "frmMain" class is coming from
It's coming from the code you have written. You posted some code that in main() tries to instantiate a frmMain class and it fails. This is strictly a C++ issue, if you can't handle it then there is nothing we can do, we will not teach you C++ -- get a good C++ book, read it and learn from it.

StarRocks
3rd January 2013, 12:31
Dear wysota,

Thanks for the reply.......Actually i am not knowing where this "frmMain" coming from because the name of my class is "Contact" but when i give that name its throwing me out with errors.....what exactly is "frmMain" ..........can you help me out in this.........Thanks in advance.........Any solution would be appreciable........


Regards,

Santosh Reddy
3rd January 2013, 14:15
Have look at the example in my other post, see it looks similar to what you are looking for.

http://www.qtcentre.org/threads/52586-XML-Serialization-of-Qt-Objects?p=235730#post235730

wysota
3rd January 2013, 16:01
Have look at the example in my other post, see it looks similar to what you are looking for.

Maybe because StarRocks and sai_3289 share the same IP :)

StarRocks
4th January 2013, 05:00
Dear Wysota,

Thanks for the reply.......i know that i am missed with "frmMain" class but this class is not part my class because the name of my class is "Contact" and am not knowing what exactly frmMain is ........Because if i use my class name Contact in the place of frmMain in the main.cpp its throwing me errors saying "error: expected type-specifier before 'frmMain'" .....Hope you got my problem......Thanks in advance....Any solution would be appreciable......



Regards,

wysota
4th January 2013, 09:44
i know that i am missed with "frmMain" class but this class is not part my class because the name of my class is "Contact" and am not knowing what exactly frmMain is

So who wrote your main() function?

StarRocks
4th January 2013, 10:21
Dear wysota,


Thanks for the reply...........Actually i google searched and got this program but as i said when i tried to apply my class name in place of "firName" its throwing me with errors......Hope you got my problem......Any solution would be appreciable....Thanks in Advance......



Regards,

Santosh Reddy
4th January 2013, 10:43
i tried to apply my class name in place of "firName" its throwing me with errors
This can be only done in a case where firName (or frmMain) is a class template, unfortunately it is not.

As wysota posted earlier, you better clear your C++ basics and them come back Xml/Objects/Qt. It will be very difficult to undersand things unless one is clear about what they are writing (in C++). I think this is one of the reasons this thread grown so long in couple of days:)

wysota
4th January 2013, 10:44
I'm sorry but with no C++ skills you will not be able to write any programs. Be it using Qt or not. Go and learn basic stuff first.

d_stranz
4th January 2013, 16:55
So who wrote your main() function?

Maybe it was sai_3289. Both of them seem to be using the same keyboard, the one with the sticky ................................. key.