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

.h文件

#pragma once

#include 
#include 
#include 
#include 
#include 

#define TYPE_CLASS_LIST(Class)        typedef QList 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 XmlAttribute & attr);
     QDomElement CreateRoot(const QString & node, const XmlAttributeList & attrs);

     QDomElement WriteDomNode(QDomElement & element, const QString & nodeName);
     QDomElement WriteDomElement(QDomElement & element, const QString & nodeName, const XmlAttribute & attr);
     QDomElement WriteDomElementText(QDomElement & element, const QString & nodeName, const QString & text);

     //向结点添加元素
     void WriteAttribute(QDomElement & element, const XmlAttribute & attr);
private:
     QFile * _File;
     QDomDocument * _Document;
};

.cpp

#include "XmlHelper.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


XmlHelper::XmlHelper(QObject *parent)
    : QObject(parent),
      _File(nullptr),
      _Document(nullptr)
{

}

XmlHelper::~XmlHelper()
{
    delete _File;
    delete _Document;
}



bool XmlHelper::CreateXmlFile(const QString &filePath)
{
    if(_File != nullptr)
    {
        delete _File;
        _File = nullptr;
    }
    _File = new QFile(filePath);
    _Document = new QDomDocument;

    if(!_File->open(QIODevice::WriteOnly))
    {
        _File->close();
        return false;
    }
    _File->close();
    return true;
}

bool XmlHelper::WriteToXmlFile(const QString &filePath)
{
    delete _File;
    _File = nullptr;
    //delete _Document;

    _File = new QFile(filePath);
   //_Document = new QDomDocument;

    if(!_File->open(QFile::WriteOnly))
    {
        return false;
    }
    QTextStream out(_File);
    _Document->save(out, 4);
    _File->close();
    return true;
}

int XmlHelper::ReadXmlFile(const QString &filePath)
{
    _File = new QFile(filePath);
    _Document = new QDomDocument;

    if(!_File->open(QIODevice::ReadOnly))
    {
        qDebug() << "open error";
        _File->close();
        return 1;
    }

    if(!_Document->setContent(_File))
    {
        qDebug() << "setContent error";
        _File->close();
        delete _File;
        _File = nullptr;
        return 1;
    }
    _File->close();
    return 0;
}

QString XmlHelper::GetElementTextByTagName(const QString &tagNmae)
{
    const QDomNodeList &nodeList = _Document->elementsByTagName(tagNmae);
    if(nodeList.isEmpty())
    {
        return QString();
    }
    return nodeList.at(0).toElement().text();
}

QString XmlHelper::GetElementAttributeByTagName(const QString &tagName, const QString &attrName)
{
    const QDomNodeList &nodeList = _Document->elementsByTagName(tagName);
    if(nodeList.isEmpty())
    {
        return QString();
    }
    return nodeList.at(0).toElement().attributeNode(attrName).value();
}


SubNodeList XmlHelper::GetSubNodeListByTagName(const QString &tagName)
{
    QDomNodeList nodeList = _Document->elementsByTagName(tagName);
    SubFileList list;
    if(nodeList.isEmpty())
    {
        return QList();
    }
    for(int i = 0; i < nodeList.count(); i++)
    {
        QDomNode node = nodeList.at(i);
        QDomNodeList childNodes = node.childNodes();
        QString fileName = childNodes.at(0).toElement().text();
        QString filePath = childNodes.at(1).toElement().text();
        qint64 fileSize = childNodes.at(2).toElement().text().toInt();
        SubNode ptr = SubNode (fileName, filePath, fileSize);
        list.append(ptr);
    }
    return list;
}

QStringList XmlHelper::GetTextListByTagName(const QString &tagName)
{
    QDomNodeList nodeList = _Document->elementsByTagName(tagName);
    QStringList list;
    if(nodeList.isEmpty())
    {
        return QStringList();
    }
    for( int  i = 0; i < nodeList.count(); i++)
    {
        QDomNode node = nodeList.at(i);
        QDomNodeList childNodes = node.childNodes();
        for(int j  = 0; j < childNodes.count(); j++)
        {
            list.append(childNodes.at(j).toElement().text());
        }
    }
    return list;
}

QStringList XmlHelper::GetTextListByTagName(const QString &parentTagName, const QString &tagName)
{
    QDomNodeList nodeList = _Document->elementsByTagName(parentTagName);
    QStringList list;
    if(nodeList.isEmpty())
    {
        return QStringList();
    }
    else
    {
        QDomNode node = nodeList.at(0).namedItem(tagName);
        QDomNodeList childNodes = node.childNodes();
        if(childNodes.count() <= 1)
        {
            list.append(node.toElement().text());
        }
        else
        {
            for(int i = 0; i < childNodes.count(); i++)
            {
                list.append(childNodes.at(i).toElement().text());
            }
        }
    }
    return list;
}

void XmlHelper::CreateProcessingInstruction()
{
    const QDomNode & node = _Document->createProcessingInstruction("xml", "version = \"1.0\" encoding = \"utf-16\"");
    _Document->appendChild(node);
}

QDomElement XmlHelper::CreateRoot(const QString &node)
{
    const QDomElement & root = _Document->createElement(node);
    _Document->appendChild(root);
    return root;
}

QDomElement XmlHelper::CreateRoot(const QString &node, const XmlAttribute &attr)
{
    QDomElement root = _Document->createElement(node);
    WriteAttribute(root, attr);
    _Document->appendChild(root);
    return root;
}

QDomElement XmlHelper::CreateRoot(const QString &node, const XmlAttributeList &attrs)
{
    QDomElement root = _Document->createElement(node);
    foreach(XmlAttribute attr, attrs)
    {
        WriteAttribute(root, attr);
    }
    _Document->appendChild(root);
    return root;
}


QDomElement XmlHelper::WriteDomNode(QDomElement &element, const QString &nodeName)
{
    const QDomElement & domElement = _Document->createElement(nodeName);
    element.appendChild(domElement);
    return domElement;
}

QDomElement XmlHelper::WriteDomElement(QDomElement &element, const QString &nodeName, const XmlAttribute &attr)
{
    QDomElement domElement = WriteDomNode(element, nodeName);
    WriteAttribute(domElement, attr);
    return domElement;
}

QDomElement XmlHelper::WriteDomElementText(QDomElement &element, const QString &nodeName, const QString &text)
{
    QDomElement domElement = WriteDomNode(element, nodeName);
    const QDomText &domText = _Document->createTextNode(text);
    domElement.appendChild(domText);

    return domElement;
}

void XmlHelper::WriteAttribute(QDomElement &element, const XmlAttribute &attr)
{
    switch(QtVariantType(attr.m_value))
    {
    case QVariant::Int:
        element.setAttribute(attr.m_name, attr.m_value.toInt());
        break;
    case QVariant::String:
        element.setAttribute(attr.m_name, attr.m_value.toString());
        break;
    case QVariant::LongLong:
        element.setAttribute(attr.m_name, attr.m_value.toLongLong());
        break;
    case QVariant::ULongLong:
        element.setAttribute(attr.m_name, attr.m_value.toULongLong());
        break;
    case QVariant::Double:
        element.setAttribute(attr.m_name, attr.m_value.toDouble());
        break;
    case QVariant::UInt:
        element.setAttribute(attr.m_name, attr.m_value.toUInt());
        break;
    default:
        break;
    }
}



你可能感兴趣的:(Qt学习,qt,xml,开发语言)