Qt5 XML

 一、

1.pro文件,需要添加xml,

QT       += core gui xml

二、

<?xml version='1.0' encoding='UTF-8'?>
<data>
    <reg1>20201009</reg1>
    <reg2>20200831</reg2>
</data>

 

#include "fileutility.h"
#include <QFile>
#include <QtXml>
FileUtility::FileUtility()
{

}

FileUtility::~FileUtility()
{

}

void FileUtility::saveHardIdToXML(QString file_path, QString data)
{
    saveToXML(file_path,data,"");
}

void FileUtility::saveRegDataToXML(QString file_path, QString local_date, QString reg_data)
{
    saveToXML(file_path,local_date,reg_data);
}

bool FileUtility::updateLocalDate(QString file_path, QString local_date)
{
    QFile file(file_path);
    if(!file.exists())
    {
        return false;
    }

    if(!file.open(QIODevice::ReadOnly|QFile::WriteOnly))
    {
        return false;
    }

    QDomDocument doc;
    if(!doc.setContent(&file))
    {
        file.close();
        return false;
    }

    file.close();
    QDomElement root=doc.documentElement();
    QDomNodeList node_list=root.elementsByTagName("reg1");
    QDomNode node= node_list.at(0);
    node.firstChild().setNodeValue(local_date);

    if(!file.open(QFile::WriteOnly|QFile::Truncate))
    {
        return false;
    }

    QTextStream out_stream(&file);
    doc.save(out_stream,4); //缩进4格
    file.close();
    return true;
}

QString FileUtility::readFromXML(QString file_path)
{
    QString node_text="";
    QFile file(file_path);
    if(!file.exists())
    {
        return "" ;
    }
    file.setFileName (file_path);
    file.open(QIODevice::ReadOnly);

    QDomDocument doc;
    if(!doc.setContent(&file))
    {
        file.close();
        return "";
    }
    file.close();
    QDomElement root=doc.documentElement();
    QDomNodeList node1_list=root.elementsByTagName("reg1");
    QDomNodeList node2_list=root.elementsByTagName("reg2");
    if(node1_list.size()>0)
    {
       node_text+= node1_list.at(0).toElement().text()+",";
    }
    if(node2_list.size()>0)
    {
       node_text+= node2_list.at(0).toElement().text();
    }
    return node_text;

}


void FileUtility::saveToXML(QString file_path,QString reg1_data, QString reg2_data)
{

    QFile file(file_path);
    if(file.exists (file_path))
    {
    file.remove();
    }
    file.setFileName (file_path);
    file.open(QIODevice::WriteOnly);

    QDomDocument doc;
    //写入xml头部
    QDomProcessingInstruction instruction; //添加处理命令
    instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instruction);

    QDomElement root=doc.createElement("data");
    doc.appendChild(root);

    QDomElement reg1=doc.createElement("reg1");
    root.appendChild(reg1);

    QDomText reg1_text=doc.createTextNode(reg1_data);
    reg1.appendChild(reg1_text);

    if(reg2_data!="")
    {
        QDomElement reg2=doc.createElement("reg2");
        root.appendChild(reg2);

        QDomText reg2_text=doc.createTextNode(reg2_data);
        reg2.appendChild(reg2_text);
    }

    //输出到文件
    QTextStream out_stream(&file);
    doc.save(out_stream,4); //缩进4格
    file.close();
}

 

posted @ 2020-08-03 17:23  ike_li  阅读(222)  评论(0编辑  收藏  举报