Possible to get QDomElement from QXmlStreamWriter?

501 Views Asked by At

I'm writing a small XMPP server using qxmpp. Now I want to create a QXmppStanza and present it (as if a client had sent it) to the server and my plugins using

void QXmppServer::handleElement(const QDomElement &element)

This function requires a QDomElement and not a QXmppStanza. The only XML realted function I found in QXmppStanza and its derived classes (besides parse(...) ) is the function

void toXml(QXmlStreamWriter *writer)

I don't have experience with XML handling in qt yet, so is there a more performant way than writing the XML to a string/ByteArray, use it as input to create a new QDomElement and return its documentElement?

2

There are 2 best solutions below

0
On BEST ANSWER

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

0
On

I had a similar issue and was able to convert from a stream to a DOM element by doing something similar to what is shown below.

The first step is to stream to a byte array.

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

The second step is to set the content of a DOM document. The document's document element should be the DOM element you need.

QDomDocument temp;
if(temp.setContent(data))
    QDomElement element = temp.documentElement(); // do whatever you want with this element