2014-02-13 37 views
2

我有我从QByteArray中加载到QDomDocument XMPP的智商,但我需要它作为QDomElementQDomDocument到QDomElement皈依

<iq from='users.netlab.cz' to='[email protected]/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> 

所以我只是用

QDomElement element = doc.toElement(); 

但它没有数据返回,我我不是很熟悉xml,所以我不确定这是否正确。任何人都可以告诉我如何将此文档转换为元素,或者如果它能够以某种方式直接将数据从QByteArray加载到QDomElement?

谢谢,Marek。

+0

你尝试调用'QDomDocument :: documentElement()'? – vahancho

+0

这工作谢谢。 – Ruli

+0

@Ruli欢迎来到stackoveflow。如果你找到了解决方案,只需回答你自己的问题。这是一个问答网站,而不是论坛。请不要编辑标题以包含“解决”一词。 –

回答

5

As mentioned in the comments,使用QDomNode::toElement()不起作用,因为文档本身在技术上不是一个元素。改用QDomDocument::documentElement()来获取根元素。

The QDomDocument documentation包括使用的这个例子:

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