1

我正在使用 qxmpp 编写一个小型 XMPP 服务器。现在我想创建一个 QXmppStanza 并将它(就像客户端发送它一样)呈现给服务器和我的插件使用

void QXmppServer::handleElement(const QDomElement &element)

此函数需要 QDomElement 而不是 QXmppStanza。我在 QXmppStanza 及其派生类(除了 parse(...) )中找到的唯一 XML 相关函数是函数

void toXml(QXmlStreamWriter *writer)

我还没有在 qt 中处理 XML 的经验,所以有没有比将 XML 写入字符串/字节数组更高效的方法,使用它作为输入来创建一个新的 QDomElement 并返回它的 documentElement?

4

2 回答 2

0

After doing some further research I have to accept it is not possible.

As stated in QDomDocument's documentation I always require a QDomDocument in order to work with a QDomElement (and other nodes):

Since elements, text nodes, comments, processing instructions, etc., cannot exist outside the context of a document (...)

The QXmlStreamWriter doesn't have a QDomDocument, so I really have to create a QDomDocument (which of course must live as long I want to work with the element) and then parse the text (QDomDocument::setContent).

于 2014-07-02T12:06:12.217 回答
0

我遇到了类似的问题,并且能够通过执行类似于下面显示的操作将流转换为 DOM 元素。

第一步是流式传输到字节数组。

QByteArray data;
QXmlStreamWriter writer(&data);
object->toXml(&writer);

第二步是设置 DOM 文档的内容。文档的文档元素应该是您需要的 DOM 元素。

QDomDocument temp;
if(temp.setContent(data))
    QDomElement element = temp.documentElement(); // do whatever you want with this element
于 2017-03-28T18:57:16.030 回答