我在使用 pugiXml 解析 xml 文档时遇到问题,在我看来一切都是正确的,但这段代码不起作用:(
void MainWindow::open()
{
QString fileName = QFileDialog::getOpenFileName(this,"Open");
xml_document doc;
doc.load_file(fileName.toStdString().c_str());
for (pugi::xml_node node : doc.child("Person"))
{
qDebug(node.child_value("nom"));
qDebug(node.child_value("Age"));
}
}
Xml文件格式:
<?xml version="1.0"?>
<Persons>
<Person>
<nom>Med</nom>
<Age>12</Age>
</Person>
<Person>
<nom>Nasr</nom>
<Age>14</Age>
</Person>
<Person>
<nom>Souad</nom>
<Age>52</Age>
</Person>
</Persons>
最佳答案
最可能的原因是您应该使用 doc.child("Persons")。
您案例中的文档对象有一个子 Persons,它有几个 Person 子对象。 doc.child("Person") 找不到节点并返回空句柄。
话虽如此,别忘了检查 load_file 的返回值。
关于c++ - 使用 pugiXml 读取 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15689116/