C++ Qt解析XML文件
1.待读取的XML文件数据
test.xml
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
<book>
<title>朝花夕拾</title>
<writer>鲁迅</writer>
</book>
<book>
<title>C和指针</title>
<writer>KennethA.Reek</writer>
</book>
</bookstore>
2.源代码
main.cpp
#include <QCoreApplication>
#include <QFile>
#include <QDomComment>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv)

本文详细介绍了如何在C++中使用Qt库的QDomDocument、QDomElement和QDomNode类来解析XML文件。首先展示了待读取的XML文件结构,接着讲解了如何读取XML文件,创建XML文档对象,以及利用setContent函数设置文档内容。通过documentElement获取根元素,firstChild函数获取子节点。然后,通过示例代码解释了如何遍历XML文档的book节点及其子节点title和writer,解析XML数据。
最低0.47元/天 解锁文章
256

被折叠的 条评论
为什么被折叠?



