.h文件
#pragma once
#include <QObject>
#include <QtXml/QDomDocument>
#include <QtXml/QDomElement>
#include <QFile>
#include <QVariant>
#define TYPE_CLASS_LIST(Class) typedef QList<Class> Class##List
/// Variant type
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
# define QtVariantType(p) p.typeId()
#else
# define QtVariantType(p) p.type()
#endif
struct XmlAttribute
{
public:
QString m_name;
QVariant m_value;
XmlAttribute(const QString &key, const QVariant &value)
: m_name(key),
m_value(value)
{
}
};
TYPE_CLASS_LIST(XmlAttribute);
//子结点是自定义的
struct SubNode
{
public:
QString FileName;
QString FilePath;
qint64 FileSize;
SubNode(const QString & fileName, const QString & filePath, qint64 fileSize)
: FileName(fileName),
FilePath(filePath),
FileSize(fileSize)
{
}
};
TYPE_CLASS_LIST(SubNode);
class XmlHelper : public QObject
{
Q_OBJECT
public:
explicit XmlHelper(QObject *parent = nullptr);
~ XmlHelper();
public:
//创建XML文件
bool CreateXmlFile(const QString & filePath);
bool WriteToXmlFile(const QString & filePath);
int ReadXmlFile(const QString & filePath);
//解析XML
QString GetElementTextByTagName(const QString & tagNmae);
QString GetElementAttributeByTagName(const QString & tagName, const QString & attrName);
SubNodeList GetSubNodeListByTagName(const QString & tagName = "NodeName");
QStringList GetTextListByTagName(const QString & tagName);
QStringList GetTextListByTagName(const QString & parentTagName, const QString & tagName);
//添加元素结点
void CreateProcessingInstruction();
QDomElement CreateRoot(const QString & node);
QDomElement CreateRoot(const QString & node, const