如何使用 QT 中的 QDomDocument 对象将字符串读取为 XML

发布于 2024-12-28 20:52:46 字数 3114 浏览 1 评论 0 原文

我使用此函数接收作为 XML 文档的字符串:

void HttpWindow::replyFinished(QNetworkReply *reply)
{
     QString data = reply->readAll().trimmed();
     bool value=document->setContent(&data);
}

这是我的插槽和信号:

connect(reply, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));

当我输入上面的代码时,我收到错误消息,没有预定义的函数可以接收我发送的一个字符串...有人可以尝试吗纠正这个问题,这样我就可以看到如何将此 Qstring 值放入 xml 文档中。所以我可以阅读 XML

谢谢

编辑: 这些是我必须选择的功能,我已经尝试了所有功能,但似乎我没有发送正确的参数:

 // 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 );

编辑2: 我无法编译它,我可以给你这段代码:

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*)

或者告诉我如何获得 Qdebug 输出。你的意思是像 Qdebug<

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活泼老夫 2025-01-04 20:52:46

QDomDocument::setContent() 确实没有重载,它采用 QString 指针,按值传递将修复错误。

然而,您的代码中还有另一个严重的问题,即您正在尝试从 QByteArray (readAll() 的结果)转换为 QString。这不仅是不必要的,而且是危险的,因为转换是在假设内容是 ascii 的情况下完成的。您只需调用 document->setContent(reply) 即可。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文