最可能的解释是你做错了什么,QXmlStreamReader
在解析文档时不应该跳过内部元素。您还没有提供任何源代码,因此无法说出您到底做错了什么。
这是我的代码示例,它非常适用于与您在 macOS 10.13.2 上使用 Qt 5.9.2 的示例非常相似:
#include <QCoreApplication>
#include <QDebug>
#include <QXmlStreamReader>
#include <QFile>
#include <QHash>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if (argc != 2) {
qWarning() << "Usage: " << argv[0] << " <file>";
return 1;
}
QFile file(argv[1]);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open file " << argv[1] << " for reading";
return 1;
}
QXmlStreamReader reader(&file);
QString currentProjectId;
QHash<QString,QString> pathByProjectId;
while(!reader.atEnd())
{
reader.readNext();
if (reader.isStartDocument()) {
continue;
}
if (reader.isEndDocument()) {
break;
}
if (reader.isStartElement())
{
QStringRef elementName = reader.name();
if (elementName == "project") {
QXmlStreamAttributes attrs = reader.attributes();
currentProjectId = attrs.value("identifier").toString();
}
else if (elementName == "path") {
pathByProjectId[currentProjectId] = reader.readElementText(QXmlStreamReader::IncludeChildElements);
}
continue;
}
}
for(auto it = pathByProjectId.constBegin(),
end = pathByProjectId.constEnd(); it != end; ++it)
{
qDebug() << "Path for project " << it.key() << ": " << it.value();
}
file.close();
return 0;
}
这是我提供给这个示例程序的稍微修改的示例:
<?xml version="1.0" encoding="UTF-8"?>
<body>
<project identifier="project1">
<author>Joe Smith</author>
<author2>Rick Jones</author2>
<path>projects/internal/project1</path>
<version>1.51</version>
</project>
<project identifier="project2">
<author>Terry Chimes</author>
<author>Janie Jones</author>
<path>projects/external/project2</path>
<version>19.77</version>
</project>
</body>
我在您的示例中添加的是 XML 版本/编码声明 + 高级body
标记,以防止QXmlStreamReader
认为第一个project
标记是整个文档的根元素。我还更改了第一个项目的路径,使其与第二个项目的路径不同。
这是我得到的输出:
Path for project "project1" : "projects/internal/project1"
Path for project "project2" : "projects/external/project2"