PDA

View Full Version : XML Serialization of Qt Objects ??



sai_3289
29th December 2012, 04:58
Hi all,
How can we do XML serialization for the Qt objects?????

class Student
{
public :
int sno=10;
Qstring name="Sai"
Qstring phno="989898111"
}

class second
{
Student stu_obj=new Student();
qdebug()<<stu_obj.sno<<stu_obj.name<<<stu_obj.phno; // Whcih prints all those details
}

Now how can i do XML serialize for this stu_obj in qt ,so that we can transfer for service api over web?????

ChrisW67
29th December 2012, 06:41
Any way you like: manually write strings, use QXmlStreamWriter, or DomDocument.

sai_3289
2nd January 2013, 05:07
Thanks for reply....No its not possible because the Service API which i need to hit/access accepts only xml/json serialized object ....is this Posisble in Qt or not plz give me some idea on dis ???? like we have in .net(http://support.microsoft.com/kb/815813) or Java.......??

Lesiok
2nd January 2013, 07:37
Mr Google is Yours friend. Look at this (http://blog.mathieu-leplatre.info/access-a-json-webservice-with-qt-c.html) and this (http://qjson.sourceforge.net/).

anda_skoa
2nd January 2013, 14:20
No its not possible because the Service API which i need to hit/access accepts only xml/json serialized object

If it accepts XML serialied objects than of course this is possible by the option suggested by ChrisW67. All of those options, especially QXmlStreamWriter and QDomDocument, produce XML.

You can easily try those approaches stand-alone, e.g. creating an XML document and writing it to a file instead of sending it to a server.

Cheers,
_

sai_3289
3rd January 2013, 07:17
Thanks for reply.....
creating an XML document and writing it to a file or parsing an xml file has been done successfully by using 2 ways i.e QxmlStreamWriter/Reader and QDomDocument.
Like we have in .net(http://support.microsoft.com/kb/815813) ,IS DIS POSSIBLE IN QT OF SERIALIZING an OBJECT to XML or NOT ????
Here my requirement is to synch to server at any given time,using provided web service api.

Santosh Reddy
3rd January 2013, 07:27
This is not directly possible in Qt (I mean Qt does not have such API).

Yes is defenetly possible in Qt you need to write the XML by youself using the classes you already found.

sai_3289
3rd January 2013, 08:53
Thank a lot for d reply.....
Ok after writing required xml..how do we can post that to web service API.....Where My service API accepts only xml type objects...

Lesiok
3rd January 2013, 10:30
sai_3289, do You check out the links on the post No. 4 ?

sai_3289
3rd January 2013, 12:20
Thank a lot for d response........Yeah lesiok....in that post no 4 accessing the webservice is done by using query string means binding the values to url directly....
But if we need to pass around 15 values or more it's not possible because we get an error as querystring lenght exceeds.
so we need to make use of object serialization concept here and which i need it.. ....or is der any other way to solve???

Santosh Reddy
3rd January 2013, 14:05
But if we need to pass around 15 values or more it's not possible because we get an error as querystring lenght exceeds
Where did you get the error from?

Dear sai_3289, the link you referred in earlier post is related to converting the state of object (C# object to be precise) in to a serial stream so that it can be saved/transmitted in to a database/server/file/memory/console/where ever you want it to go. One important point here is that serialization format is specific to C# object types. Note the the link says "Serialization is the process of taking the state of an object and persisting it in some fashion". The last couple of words are important

Now, if I understood your post/question correctly, you are tying to convert a C++ (may be a QT C++ objcet) into a serial stream. Well you can do it but don't expect it work with your C#.Net application.

Here is just an example



class clsPerson
{
public:
QString FirstName;
QString MI;
QString LastName;
};

clsPerson p;
p.FirstName = "Jeff";
p.MI = "A";
p.LastName = "Price";

QString serial;
QXmlStreamWriter stream(&serial);
stream.setAutoFormatting(true);
stream.writeStartDocument();

stream.writeStartElement("clsPerson");
stream.writeAttribute("xmlns:xsi", "?");
stream.writeAttribute("xmlns:xsd", "?");

stream.writeTextElement("FirstName",p.FirstName);
stream.writeTextElement("MI",p.MI);
stream.writeTextElement("LastName",p.LastName);

stream.writeEndElement();

stream.writeEndDocument();

qDebug() << serial.toStdString().c_str();

anda_skoa
7th January 2013, 13:15
But if we need to pass around 15 values or more it's not possible because we get an error as querystring lenght exceeds.

That sounds like all data is being encoded into the URL.
Did you used a GET or POST operation?



so we need to make use of object serialization concept here and which i need it.. ....or is der any other way to solve???

Given that you have the serialization covered already, the problem now lies with sending the serialized data. The Web service's API documentation should have some information about the expected operation (usually HTTP POST) and about meta data (such as required HTTP headers) that needs to be present.

Cheers,
_