QDomDocument到QDomElement转换(QDomDocument to QDomElement conversion)

编程入门 行业动态 更新时间:2024-10-27 11:22:10
QDomDocument到QDomElement转换(QDomDocument to QDomElement conversion)

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

所以我刚才用过

QDomElement element = doc.toElement();

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

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?

最满意答案

正如评论中所提到的 ,使用QDomNode::toElement()不起作用,因为文档本身在技术上不是一个元素。 使用QDomDocument::documentElement()来获取根元素。

QDomDocument文档包含以下使用示例:

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

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

更多推荐

本文发布于:2023-07-28 22:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309783.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:QDomDocument   QDomElement   conversion

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!