如何在Qt中使用QDomNodeList的foreach? [英] How do I use foreach with QDomNodeList in Qt?
问题描述
我是Qt新手,每天都在学习新东西。
目前,我在空闲时间为我的诺基亚N900开发一个小应用程序。
一切都很好,我可以在设备上编译和运行Maemo应用程序。
我刚刚了解到 foreach
关键字在Qt中。 (我知道它不在C ++中,所以我没有想到它,直到我不小心偶然发现了一个Qt文档提到它)。
所以,我决定改变我很烦人和不可读的循环foreach,但是我失败了:
QDomNodeList list = doc.lastChild()。childNodes()。at(1)。则firstChild()的childNodes();
for(int x = 0; x {
QDomElement node = list.at(x).toElement();
//用节点
做一些事
$ b 这是我试过的:
$ b $ foreach(childDodes())。 ))
{
//用节点
做一些事情}
由于某些原因,上面的代码甚至没有编译。我从编译器得到神秘的错误消息。
如果
如果有人请向我解释如何得到它的权利? foreach
循环不支持 QDomNodeList
,有办法处理XML文件,它支持 foreach $ c $为了说明,
$ b
> / / code>在这种情况下执行以下操作:
EveCharacter chr;
chr.setName(node.attribute(EVE_NAME));
chr.setId(node.attribute(EVE_CHARACTER_ID).toInt());
acc-> addCharacter(chr);
其中acc的类型是 EveAccount
,将数据存储在 QList< EveCharacter>
中。
大写符号是编译时常量字符串。
(我正在为EVE Online API创建一个客户端,这是从接收帐户字符XML 并解释它。)
这是我如何创建 doc
:
QDomDocument doc;
doc.setContent(reply-> readAll());
注意 reply
是一个 QNetworkReply *
,它是从 QNetworkAccessManager
返回的。
由于EVE API与XML协同工作,我在应用程序的许多地方做了很多与此类似的XML解析。
大多数XML可能有几百行,可以包含非常规数据模式,如 。
解决方案 foreach
只支持容器类,所以你不能使用它与 QDomNodeList
。
我不确定你的实际目标,但是我找到 QXmlSimpleReader
和 QXmlStreamReader
为
$ b
编辑以匹配问题编辑:你正在做的事情看起来像XPath或XQuery的主要候选人。查看 QtXmlPatterns
模块,这将给你一组字符节点,而不必循环所有其他节点。
I'm new to Qt and I'm learning something new every day.
Currently, I'm developing a small application for my Nokia N900 in my free time.
Everything is fine, I am able to compile and run Maemo applications on the device.
I've just learned about the foreach
keyword in Qt. (I know it is not in C++, so I didn't think about it until I accidentally stumbled upon a Qt doc that mentioned it.)
So, I decided to change my quite annoying and unreadable loops to foreach, but I failed with this:
QDomNodeList list = doc.lastChild().childNodes().at(1).firstChild().childNodes();
for (int x = 0; x < list.count(); x++)
{
QDomElement node = list.at(x).toElement();
// Do something with node
}
This is how I tried:
foreach (QDomElement node, doc.lastChild().childNodes().at(1).firstChild().childNodes())
{
// Do something with node
}
For some reason the above code doesn't even compile. I get cryptic error messages from the compiler.
Could someone please explain to me how to get it right?
If the foreach
loop doesn't support QDomNodeList
, is there a way to handle XML files which supports foreach
?
EDIT:
To clarify, // Do something with node
is the following in this case:
EveCharacter chr;
chr.setName(node.attribute(EVE_NAME));
chr.setId(node.attribute(EVE_CHARACTER_ID).toInt());
acc->addCharacter(chr);
Where acc is of type EveAccount
, which stores data in a QList<EveCharacter>
.
The uppercase symbols are compile-time constant strings.
(I'm creating a client for the EVE Online API. This is from the method that receives the account characters XML and interprets it.)
This is how I create doc
:
QDomDocument doc;
doc.setContent(reply->readAll());
Note that reply
is a QNetworkReply*
which is sent back from a QNetworkAccessManager
.
However, as the EVE API works with XML, I do a lot of XML parsing very similar to this in many places in my application.
Most of the XMLs can be several hundred lines long and can contain quite non-regular data patterns, such as this one.
解决方案 foreach
only supports the container classes, so you cannot use it with a QDomNodeList
.
I'm not sure of you actual goal, but I find the QXmlSimpleReader
and QXmlStreamReader
to be the easiest way to deal with XML.
Edit to match question edit:
What you are trying to do looks like a prime candidate for XPath or XQuery. Take a look at the QtXmlPatterns
module this will give you a set of character nodes without having to loop through all the other nodes.
这篇关于如何在Qt中使用QDomNodeList的foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!