QDomDocument到QDomElement转换

问题描述 投票:2回答:1

我有从QByteArray加载到QDomDocument的xmpp iq,但我需要它作为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?

qt xmpp
1个回答
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();
}
© www.soinside.com 2019 - 2024. All rights reserved.