Qt::XML文件的解析与创建

.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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值