QDomDocument读写XML文件

QDomDocument实现XML文件读写
本文介绍了如何使用Qt的QDomDocument类进行XML文件的读写操作。首先,通过XMLSpy设计XML文件结构,然后展示了利用QDomDocument进行XML文件的创建和读取的具体代码和函数,包括保存XML文件的方法。

XML文件大家应该都很熟悉,与ini一样,xml也是常用的软件配置文件格式。

在读写一个xml文件之前, 首先要做的工作是设计好xml文件的结构。

XMLSpy这个工具在xml设计方面有显著的优势,本人使用的是XMLSpy  2015,可以上官网http://www.altova.com/download-trial.html下载,并申请试用版本的序列号。

设计完的结构如下:

设计完结构之后,可以导出例子xml文件如下:

到这里,我们的xml文件就设计好了,剩下的就是用Qt来实现xml的读写操作。

Qt中实现对xml读写操作的类是QDomDocument相关的类,一般情况下需要包含下列三个头文件:

#include <QFile>   
#include <QtXml\QtXml>
#include <QtXml\QDomDocumen
Qt中,可以使用多种方式实现XML文件读写操作,其中最常用的方式包括`QDomDocument`、`QXmlStreamReader`、`QXmlStreamWriter`和`Qt XML Patterns`模块。以下是具体的实现方法及其示例代码。 ### 使用 QDomDocument 读写 XML 文件 `QDomDocument` 提供了基于 DOM 的 XML 文件操作方式,适用于需要频繁修改文档结构的场景,但内存占用较高。 #### 写入 XML 文件示例 ```cpp #include <QDomDocument> #include <QFile> #include <QTextStream> void writeXmlFile(const QString &fileName) { QDomDocument doc; QDomElement root = doc.createElement("Root"); doc.appendChild(root); QDomElement child = doc.createElement("Child"); child.setAttribute("name", "Example"); QDomText text = doc.createTextNode("This is an example."); child.appendChild(text); root.appendChild(child); QFile file(fileName); if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { QTextStream stream(&file); stream << doc.toString(); file.close(); } } ``` #### 读取 XML 文件示例 ```cpp void readXmlFile(const QString &fileName) { QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) return; QDomDocument doc; if (!doc.setContent(&file)) { file.close(); return; } file.close(); QDomElement root = doc.documentElement(); QDomNode node = root.firstChild(); while (!node.isNull()) { QDomElement element = node.toElement(); if (!element.isNull()) { qDebug() << "Tag name:" << element.tagName(); qDebug() << "Attribute name:" << element.attribute("name"); qDebug() << "Text:" << element.text(); } node = node.nextSibling(); } } ``` ### 使用 QXmlStreamWriter 写入 XML 文件 `QXmlStreamWriter` 是一种流式写入方式,适合处理大型 XML 文件,因为它具有较低的内存占用[^3]。 ```cpp #include <QFile> #include <QXmlStreamWriter> void writeXmlWithStreamWriter(const QString &fileName) { QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) return; QXmlStreamWriter xml(&file); xml.setAutoFormatting(true); xml.writeStartDocument(); xml.writeStartElement("Root"); xml.writeStartElement("Child"); xml.writeAttribute("name", "StreamExample"); xml.writeCharacters("This is a stream-based example."); xml.writeEndElement(); // Child xml.writeEndElement(); // Root xml.writeEndDocument(); file.close(); } ``` ### 使用 QXmlStreamReader 读取 XML 文件 `QXmlStreamReader` 提供了一种高效的流式读取 XML 文件的方式,适合处理大型 XML 文件[^3]。 ```cpp #include <QFile> #include <QXmlStreamReader> void readXmlWithStreamReader(const QString &fileName) { QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) return; QXmlStreamReader xml(&file); while (!xml.atEnd()) { QXmlStreamReader::TokenType token = xml.readNext(); if (token == QXmlStreamReader::StartElement) { if (xml.name() == "Child") { QXmlStreamAttributes attributes = xml.attributes(); if (attributes.hasAttribute("name")) { qDebug() << "Attribute name:" << attributes.value("name").toString(); } } } else if (token == QXmlStreamReader::Characters && !xml.isWhitespace()) { qDebug() << "Text:" << xml.text().toString(); } } file.close(); } ``` ### 使用 Qt XML Patterns 模块进行复杂查询 `Qt XML Patterns` 模块支持使用 XQuery 和 XPath 进行复杂的 XML 数据查询,但学习曲线较陡[^3]。 ```cpp #include <QFile> #include <QXmlQuery> #include <QXmlResultItems> #include <QDebug> void queryXmlWithXQuery(const QString &fileName) { QXmlQuery query; QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) return; query.setFocus(QUrl::fromLocalFile(file.fileName())); query.setQuery("for $x in doc('')//Child return $x/@name"); if (query.isValid()) { QXmlResultItems result; query.evaluateTo(&result); QXmlItem item; while (!(item = result.next()).isNull()) { if (item.isAtomic()) qDebug() << "Attribute name:" << item.toAtomicValue().toString(); } } file.close(); } ``` ### 总结 - **QDomDocument**:适合小型 XML 文件,提供树形结构便于操作,但内存占用较高。 - **QXmlStreamWriter / QXmlStreamReader**:推荐用于大型 XML 文件,具有较低的内存占用和高效的流式处理能力。 - **Qt XML Patterns**:适用于需要复杂查询的场景,支持 XQuery 和 XPath,但学习难度较高[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值