gpt4 book ai didi

c++ - 在 Qt 中将 XML 导入 DOM 树

转载 作者:行者123 更新时间:2023-11-28 03:50:22 25 4
gpt4 key购买 nike

我在 Mac OS X 上使用 Qt 4.7,我有一个包含 XML 文件路径的 QString。我想将该文件导入 DOM 树并将数据作为成员变量存储到类中。做这个的最好方式是什么?

我一直在查看 QtXml文档,但我找不到从 QXml* 类转换为 QDom* 类的明确方法。

最佳答案

我认为您不需要费心使用 QXml* 类来遍历 DOM。

QDomDocument 类有一个可以获取打开的 QFile 的 setContent() 方法。

There's a code sample在 QDomDocument 文档的“详细信息”部分。

QDomDocument doc("mydocument");
QFile file("mydocument.xml");
if (!file.open(QIODevice::ReadOnly))
return;
if (!doc.setContent(&file)) {
file.close();
return;
}
file.close();

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

关于c++ - 在 Qt 中将 XML 导入 DOM 树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5725272/

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