How to read string as XML with QDomDocument object in QT
Question
I am receiving string as a XML document with this function:
void HttpWindow::replyFinished(QNetworkReply *reply)
{
QString data = reply->readAll().trimmed();
bool value=document->setContent(&data);
}
and here is my slot and signal:
connect(reply, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
When I put above code I get error that there is not pre-defined function that receive one string as I have sended...Can somone try to correct this so I can see how to put this Qstring value into xml document. So I can read XML
Thank you
EDIT: these are the functions that I have to choose, I have tried all but it seems I am not sending right paremeters:
// Qt extensions
bool setContent(const QByteArray& text, bool namespaceProcessing, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0 );
bool setContent(const QString& text, bool namespaceProcessing, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0 );
bool setContent(QIODevice* dev, bool namespaceProcessing, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0 );
bool setContent(QXmlInputSource *source, bool namespaceProcessing, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0 );
bool setContent(const QByteArray& text, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0 );
bool setContent(const QString& text, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0 );
bool setContent(QIODevice* dev, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0 );
bool setContent(QXmlInputSource *source, QXmlReader *reader, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0 );
EDIT 2: I can't compile it, I can give you this code:
httpwindow.cpp: In member function 'void HttpWindow::replyFinished(QNetworkReply*)':
httpwindow.cpp:140: error: no matching function for call to 'QDomDocument::setContent(QString*)'
..\..\..\..\Desktop\Qt\4.7.4\mingw\include/QtXml/qdom.h:343: note: candidates are: bool QDomDocument::setContent(const QByteArray&, bool, QString*, int*, int*)
..\..\..\..\Desktop\Qt\4.7.4\mingw\include/QtXml/qdom.h:344: note: bool QDomDocument::setContent(const QString&, bool, QString*, int*, int*)
..\..\..\..\Desktop\Qt\4.7.4\mingw\include/QtXml/qdom.h:345: note: bool QDomDocument::setContent(QIODevice*, bool, QString*, int*, int*)
..\..\..\..\Desktop\Qt\4.7.4\mingw\include/QtXml/qdom.h:346: note: bool QDomDocument::setContent(QXmlInputSource*, bool, QString*, int*, int*)
..\..\..\..\Desktop\Qt\4.7.4\mingw\include/QtXml/qdom.h:347: note: bool QDomDocument::setContent(const QByteArray&, QString*, int*, int*)
..\..\..\..\Desktop\Qt\4.7.4\mingw\include/QtXml/qdom.h:348: note: bool QDomDocument::setContent(const QString&, QString*, int*, int*)
..\..\..\..\Desktop\Qt\4.7.4\mingw\include/QtXml/qdom.h:349: note: bool QDomDocument::setContent(QIODevice*, QString*, int*, int*)
..\..\..\..\Desktop\Qt\4.7.4\mingw\include/QtXml/qdom.h:350: note: bool QDomDocument::setContent(QXmlInputSource*, QXmlReader*, QString*, int*, int*)
Or tell me what to do to get that Qdebug output. You mean like Qdebug<<data
?
Solution
There is indeed no overload of QDomDocument::setContent() which takes a QString pointer, passing by value would fix the error.
However there is another serious issue in your code which is that you are trying to convert from a QByteArray (the result of readAll()) to a QString. This is not only unnecessary but dangerous, since the conversion is done assuming the content is ascii. You can just call document->setContent(reply).