Serialization Of Xml using QObjects
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,
Re: Serialization Of Xml using QObjects
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
Re: Serialization Of Xml using QObjects
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,
Re: Serialization Of Xml using QObjects
I meant that Qt already has enough support so that one can write serialization (XML or any other type) them selfs
Re: Serialization Of Xml using QObjects
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,
Re: Serialization Of Xml using QObjects
It depends, what do you want to do with the serialized XML data?
Re: Serialization Of Xml using QObjects
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,
Re: Serialization Of Xml using QObjects
It is not clear to me.
Who will read the DB (the Qt Applciation / .Net serialization API)?
Re: Serialization Of Xml using QObjects
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,
Re: Serialization Of Xml using QObjects
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.
Re: Serialization Of Xml using QObjects
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,
Re: Serialization Of Xml using QObjects
I am not sure of Service(Api), whatever it is, find out the XML scema it requires and useQDomDocument to prepare the XML data. Refer Qt docs for QDomDocument, it shows how to creare XML.
Re: Serialization Of Xml using QObjects
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.
Re: Serialization Of Xml using QObjects
Lets assume we have a database query and we want to format it in XML as a table. That would be something like this:
Code:
QXmlStreamWriter writer( &xmlText );
writer.writeStartDocument( "1.0" );
writer.writeStartElement( "table" );
while ( query.next() ) {
writer.writeStartElement( "row" );
for ( int col = 0; col < row.count(); ++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,
_
Re: Serialization Of Xml using QObjects
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,
Re: Serialization Of Xml using QObjects
What exactly of what anda_skoa provided is not clear to you? The code is pretty much self explanatory.
Re: Serialization Of Xml using QObjects
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,
Re: Serialization Of Xml using QObjects
Quote:
Originally Posted by
StarRocks
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.
1 Attachment(s)
Re: Serialization Of Xml using QObjects
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
Re: Serialization Of Xml using QObjects
You forgot to include the file that defines the "frmMain" class.