使用单例类模板实现的对XML文件的节点、属性、文本进行增删改查,可以直接用!
直接POST代码,比较简单好用。
头文件
#pragma once
#include "SingletonCRTP.h"
#include <stdio.h>
#include <iostream>
#include <QObject>
#include <QXmlStreamReader>
#include <QFile>
#include <QtXml\QDomComment>
#include <QDir>
#include <QTextStream>
#include <QCoreApplication>
class XmlHelper: public SingletonCRTP<XmlHelper>
{
friend class SingletonCRTP<XmlHelper>;
public:
explicit XmlHelper(const QString& xmlFilePath);
XmlHelper() {};
bool loadXml(const QString& filePath);
QString getNode(const QString& nodeName) const;
QStringList getNodes(const QString& nodeName) const;
QStringList getChildNodes(const QString& nodeName) const;
QStringList getNodes(const QString& nodeName, const QString& attrName) const;
QString getAttribute(const QString& nodeName, const QString& attrName) const;
QStringList getAttributes(const QString& nodeName, const QString& attrName) const;
QString getText(const QString& nodeName) const;
bool setNodeText(const QString& nodeName, const QString& text);
bool setNodeAttribute(const QString& nodeName, const QString& attrName, const QString& attrValue);
bool addNode(const QString& nodeName, const QString& text);
bool removeNode(const QString& nodeName);
bool saveToFile(const QString& filePath);
bool save();
private:
QDomDocument xmldoc;
QFile xmlFile;
QDomNode findFirstNode(const QString& nodeName) const;
};
源文件
#include "XmlHelper.h"
#include <QDomElement>
#include <QDomNodeList>
#include <QXmlStreamReader>
XmlHelper::XmlHelper(const QString& xmlFilePath)
{
loadXml(xmlFilePath);
}
bool XmlHelper::loadXml(const QString& filePath)
{
xmlFile.setFileName(filePath);
if (!xmlFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
return false;
}
QXmlStreamReader xmlReader(&xmlFile);
if (!xmldoc.setContent(&xmlReader, true))
{
xmlFile.close();
return false;
}
xmlFile.close();
return true;
}
QDomNode XmlHelper::findFirstNode(const QString& nodeName) const
{
QDomNodeList nodeList = xmldoc.elementsByTagName(nodeName);
if (!nodeList.isEmpty())
{
return nodeList.at(0);
}
return QDomNode();
}
QString XmlHelper::getNode(const QString& nodeName) const
{
QDomNode node = findFirstNode(nodeName);
if (!node.isNull())
{
QDomElement element = node.toElement();
return element.tagName();
}
return QString();
}
QStringList XmlHelper::getNodes(const QString& nodeName) const
{
QStringList nodeNames;
QDomNodeList nodeList = xmldoc.elementsByTagName(nodeName);
for (int i = 0; i < nodeList.size(); ++i)
{
QDomElement element = nodeList.at(i).toElement();
nodeNames.append(element.tagName());
}
return nodeNames;
}
QStringList XmlHelper::getChildNodes(const QString& nodeName) const
{
QStringList nodeNames;
QDomNodeList nodeList = xmldoc.elementsByTagName(nodeName);
for (int i = 0; i < nodeList.size(); ++i)
{
QDomElement element = nodeList.at(i).toElement();
QDomNodeList childs = element.childNodes();
for (int j = 0; j < childs.size(); ++j)
{
if (childs.at(j).toElement().tagName() != "")
{
nodeNames.append(childs.at(j).toElement().tagName());
}
}
}
return nodeNames;
}
QStringList XmlHelper::getNodes(const QString& nodeName, const QString& attrName) const
{
QStringList nodeNames;
QDomNodeList nodeList = xmldoc.elementsByTagName(nodeName);
for (int i = 0; i < nodeList.size(); ++i)
{
QDomElement element = nodeList.at(i).toElement();
if (element.attribute("name") == attrName)
{
nodeNames.append(element.tagName());
}
}
return nodeNames;
}
QString XmlHelper::getAttribute(const QString& nodeName, const QString& attrName) const
{
QDomNode node = findFirstNode(nodeName);
if (!node.isNull())
{
QDomElement element = node.toElement();
return element.attribute(attrName);
}
return QString();
}
QStringList XmlHelper::getAttributes(const QString& nodeName, const QString& attrName) const
{
QStringList attrValues;
QDomNodeList nodeList = xmldoc.elementsByTagName(nodeName);
for (int i = 0; i < nodeList.size(); ++i)
{
QDomElement element = nodeList.at(i).toElement();
if (element.hasAttribute(attrName))
{
attrValues.append(element.attribute(attrName));
}
}
return attrValues;
}
QString XmlHelper::getText(const QString& nodeName) const
{
QDomNode node = findFirstNode(nodeName);
if (!node.isNull())
{
QDomElement element = node.toElement();
return element.text();
}
return QString();
}
bool XmlHelper::setNodeText(const QString& nodeName, const QString& text)
{
QDomNode node = findFirstNode(nodeName);
if (!node.isNull())
{
QDomNode oldnode = node.firstChild();
node.firstChild().setNodeValue(text);
QDomNode newnode = node.firstChild(); //值修改过后
node.replaceChild(newnode, oldnode); //调用节点的replaceChild方法实现修改功能
return true;
}
return false;
}
bool XmlHelper::setNodeAttribute(const QString& nodeName, const QString& attrName, const QString& attrValue)
{
QDomNode node = findFirstNode(nodeName);
if (!node.isNull())
{
QDomElement element = node.toElement();
element.setAttribute(attrName, attrValue);
return true;
}
return false;
}
bool XmlHelper::addNode(const QString& nodeName, const QString& text)
{
QDomElement root = xmldoc.documentElement();
QDomElement newNode = xmldoc.createElement(nodeName);
newNode.appendChild(xmldoc.createTextNode(text));
root.appendChild(newNode);
return true;
}
bool XmlHelper::removeNode(const QString& nodeName)
{
QDomNodeList nodeList = xmldoc.elementsByTagName(nodeName);
for (int i = 0; i < nodeList.size(); ++i)
{
QDomNode node = nodeList.at(i);
node.parentNode().removeChild(node);
}
return true;
}
bool XmlHelper::saveToFile(const QString& filePath)
{
QFile file(filePath);
if (!file.open(QFile::WriteOnly | QFile::Truncate | QFile::Text))
{
return false;
}
try
{
QTextStream stream(&file);
stream.setCodec("utf-8");
xmldoc.save(stream, 4, QDomNode::EncodingFromTextStream);
file.close();
return true;
}
catch (const std::exception&)
{
return false;
}
}
bool XmlHelper::save()
{
if (!xmlFile.open(QFile::WriteOnly | QFile::Truncate | QFile::Text))
{
return false;
}
try
{
QTextStream stream(&xmlFile);
stream.setCodec("utf-8");
xmldoc.save(stream, 4, QDomNode::EncodingFromTextStream);
xmlFile.close();
return true;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
return false;
}
}