将 QXmlItem 转换为 QtDomElement 或类似的?
我正在解析一个相当复杂的 XML 文件,其结构如下:
<root>
...
...
<item>
<subitem id="1"/>
<text>
text1
</text>
</item>
<item>
<subitem id="2"/>
<text>
text2
</text>
</item>
...
<item>
...
</item>
...
</root>
虽然很粗糙,但我希望您能明白我的意思。我主要对“项目”节点感兴趣。所以我编写了以下代码(直接来自 Qt 的在线手册):
QXmlQuery query;
query.setQuery("//item/");
QXmlResultItems result;
query.evaluateTo(&result);
QXmlItem item(result.next());
while (!item.isNull())
{
if (item.isNode())
{
// WHAT DO I DO NOW?
}
item = result.next();
}
现在,QXmlItem 似乎代表两个概念,一个文字值(如字符串)或一个 Node(这就是 item.isNode() 正在做的事情)。不幸的是,我无法掌握如何将 QXmlItem 转换为可再次查询的内容。特别是在上面的示例中,我想获取“id”属性和文本元素。我可以使用 XQuery 方法来完成此操作,还是我在这里偏离了基础?
有什么建议吗?
谢谢!
I'm parsing a fairly complicated XML file of the following structure:
<root>
...
...
<item>
<subitem id="1"/>
<text>
text1
</text>
</item>
<item>
<subitem id="2"/>
<text>
text2
</text>
</item>
...
<item>
...
</item>
...
</root>
It's pretty crude but you get my drift I hope. I'm primarily interested in "item" nodes. So I wrote the following code (directly out of the Qt's online manual):
QXmlQuery query;
query.setQuery("//item/");
QXmlResultItems result;
query.evaluateTo(&result);
QXmlItem item(result.next());
while (!item.isNull())
{
if (item.isNode())
{
// WHAT DO I DO NOW?
}
item = result.next();
}
Now, QXmlItem appears to represent two concepts, a literal value (like a string) or a Node, (which is what item.isNode() is doing). Unfortunately, I can't grasp how to convert the QXmlItem to something that will query-able again. In particular from the example above I'd like to grab the "id" attribute, and the text element. Can I do this using the XQuery approach, or am I way off base here?
Any advice?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 QXmlItem 修改查询的焦点。例如:
将检索
的
值。You can use QXmlItem to modify the focus of your query. For example:
will retrieve the
<text>
value of the<item>
.QXmlQuery
是一篇糟糕的 Qt 文档,但我想说你编写查询来返回你真正想要的项目,即(这是一个未经教育的猜测)W3Schools 有一个 XPath 教程可能会有所帮助
QXmlQuery
is one crummy piece of Qt documentation, but i would say that you write your query to return the items that you actually want i.e. (this is an uneducated guess)W3Schools has a Tutorial on XPath that might help