gpt4 book ai didi

c++ - 将 QXmlItem 转换为 QtDomElement 或类似的?

转载 作者:数据小太阳 更新时间:2023-10-29 02:55:18 25 4
gpt4 key购买 nike

我正在解析一个具有以下结构的相当复杂的 XML 文件:

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

现在,QXmlItem 似乎表示两个概念,文字值(如字符串)或节点(这就是 item.isNode() 正在做的事情)。不幸的是,我无法掌握如何将 QXmlItem 转换为可以再次查询的内容。特别是在上面的示例中,我想获取“id”属性和文本元素。我可以使用 XQuery 方法来执行此操作,还是我偏离了这里的基础?

有什么建议吗?

谢谢!

最佳答案

您可以使用 QXmlItem 来修改查询的焦点。例如:

QXmlItem item(result.next());
while (!item.isNull())
{
if (item.isNode())
{
query.setFocus(item);
query.setQuery("./text/string()");
QString text;
query.evaluateTo(&text);
}
item = result.next();
}

将检索 <text> <item> 的值(value).

关于c++ - 将 QXmlItem 转换为 QtDomElement 或类似的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2429464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com