Qt 封装xml操作类,实现节点的增删改查操,初始化等
时间: 2023-08-16 09:06:29 浏览: 226
Qt操作XML文档(增删改查)
好的,我可以为您提供一个简单的基于 Qt 的封装 XML 操作类的示例代码,实现节点的增删改查和初始化等功能。
首先,我们需要在头文件中引入 Qt 的相关库和命名空间:
```cpp
#include <QtCore/QFile>
#include <QtCore/QXmlStreamReader>
#include <QtCore/QXmlStreamWriter>
#include <QtCore/QDebug>
#include <QtCore/QList>
#include <QtCore/QString>
#include <QtCore/QXmlStreamAttributes>
#include <QtCore/QXmlStreamNamespaceDeclarations>
```
然后,我们定义一个类来封装 XML 操作,包括节点的增删改查和初始化等功能。以下是一个简单的示例类:
```cpp
class XmlManager {
public:
XmlManager(const QString &fileName);
virtual ~XmlManager();
void initXmlDocument();
void readXmlDocument();
void writeXmlDocument();
void addNode(const QString &parentNodeName, const QString &nodeName, const QString &nodeValue);
void removeNode(const QString &nodeName);
void updateNode(const QString &nodeName, const QString &newValue);
QString getNodeValue(const QString &nodeName);
private:
QFile m_file;
QXmlStreamReader m_reader;
QXmlStreamWriter m_writer;
QString m_fileName;
};
```
在类的构造函数中,我们需要打开 XML 文件,并初始化 `QXmlStreamReader` 和 `QXmlStreamWriter` 以进行读写操作:
```cpp
XmlManager::XmlManager(const QString &fileName) : m_fileName(fileName) {
m_file.setFileName(m_fileName);
if (!m_file.open(QIODevice::ReadWrite | QIODevice::Text)) {
qDebug() << "Failed to open file " << m_fileName;
}
m_reader.setDevice(&m_file);
m_writer.setDevice(&m_file);
}
```
在类的析构函数中,我们需要关闭文件:
```cpp
XmlManager::~XmlManager() {
m_file.close();
}
```
接下来,我们实现类中的各个方法。首先是 `initXmlDocument()` 方法,该方法用于初始化一个空的 XML 文档:
```cpp
void XmlManager::initXmlDocument() {
m_writer.setAutoFormatting(true);
m_writer.writeStartDocument();
m_writer.writeEndDocument();
}
```
然后是 `readXmlDocument()` 方法,用于读取 XML 文档的所有节点:
```cpp
void XmlManager::readXmlDocument() {
while (!m_reader.atEnd() && !m_reader.hasError()) {
QXmlStreamReader::TokenType token = m_reader.readNext();
if (token == QXmlStreamReader::StartDocument) {
continue;
}
if (token == QXmlStreamReader::StartElement) {
QString name = m_reader.name().toString();
QString value = m_reader.readElementText();
qDebug() << name << ":" << value;
}
}
}
```
`writeXmlDocument()` 方法用于将更改后的 XML 文档写回文件:
```cpp
void XmlManager::writeXmlDocument() {
m_file.resize(0);
m_writer.writeEndDocument();
}
```
`addNode()` 方法用于在指定的父节点下添加一个新的子节点:
```cpp
void XmlManager::addNode(const QString &parentNodeName, const QString &nodeName, const QString &nodeValue) {
m_reader.setDevice(&m_file);
m_writer.setDevice(&m_file);
while (!m_reader.atEnd() && !m_reader.hasError()) {
QXmlStreamReader::TokenType token = m_reader.readNext();
if (token == QXmlStreamReader::StartElement) {
QString name = m_reader.name().toString();
if (name == parentNodeName) {
m_writer.writeStartElement(nodeName);
m_writer.writeCharacters(nodeValue);
m_writer.writeEndElement();
}
}
}
}
```
`removeNode()` 方法用于删除指定的节点:
```cpp
void XmlManager::removeNode(const QString &nodeName) {
m_reader.setDevice(&m_file);
m_writer.setDevice(&m_file);
while (!m_reader.atEnd() && !m_reader.hasError()) {
QXmlStreamReader::TokenType token = m_reader.readNext();
if (token == QXmlStreamReader::StartElement) {
QString name = m_reader.name().toString();
if (name == nodeName) {
m_writer.writeEndElement();
}
}
}
}
```
`updateNode()` 方法用于更新指定节点的值:
```cpp
void XmlManager::updateNode(const QString &nodeName, const QString &newValue) {
m_reader.setDevice(&m_file);
m_writer.setDevice(&m_file);
while (!m_reader.atEnd() && !m_reader.hasError()) {
QXmlStreamReader::TokenType token = m_reader.readNext();
if (token == QXmlStreamReader::StartElement) {
QString name = m_reader.name().toString();
if (name == nodeName) {
m_writer.writeStartElement(nodeName);
m_writer.writeCharacters(newValue);
m_writer.writeEndElement();
} else {
m_writer.writeStartElement(name);
m_writer.writeCharacters(m_reader.readElementText());
m_writer.writeEndElement();
}
}
}
}
```
最后是 `getNodeValue()` 方法,用于获取指定节点的值:
```cpp
QString XmlManager::getNodeValue(const QString &nodeName) {
m_reader.setDevice(&m_file);
while (!m_reader.atEnd() && !m_reader.hasError()) {
QXmlStreamReader::TokenType token = m_reader.readNext();
if (token == QXmlStreamReader::StartElement) {
QString name = m_reader.name().toString();
if (name == nodeName) {
return m_reader.readElementText();
}
}
}
return "";
}
```
以上是一个简单的基于 Qt 的封装 XML 操作类的示例代码。您可以按照您的需求进行修改和完善。
阅读全文