QDomDocument to QDomElement conversion

qt, xmpp

Solution

As mentioned in the comments, using `QDomNode::toElement()` doesn't work because the document itself isn't technically an element. Use `QDomDocument::documentElement()` to get the root element instead.

The QDomDocument documentation includes this example of use:

// print out the element names of all elements that are direct children
// of the outermost element.
QDomElement docElem = doc.documentElement();

QDomNode n = docElem.firstChild();
while(!n.isNull()) {
    QDomElement e = n.toElement(); // try to convert the node to an element.
    if(!e.isNull()) {
        cout << qPrintable(e.tagName()) << endl; // the node really is an element.
    }
    n = n.nextSibling();
}

Problem

I have xmpp iq which I loaded from QByteArray to QDomDocument, but I need it as QDomElement ``` <iq from='users.netlab.cz' to='test_soc@jabbim.sk/QXmpp' id='search0' type='result'> <query xmlns='jabber:iq:search'> <instructions>You need an x:data capable client to search</instructions> <x xmlns='jabber:x:data' type='form'> <title>Search users in users.netlab.cz</title> <instructions>blahblah</instructions> <field type='text-single' label='User' var='user'/> ... <field type='text-single' label='Organization Unit' var='orgunit'/> </x> </query> </iq> ``` so I just used ``` QDomElement element = doc.toElement(); ``` but it returned no data, I am not really familiar with xml so I'm not really sure if this is right. Anyone can tell me how to convert this document to element or if its able to directly load data from QByteArray to QDomElement somehow?

Original source