• 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();
    }
  • 相关阅读:
    VSCode打开大文件插件
    https Java SSL Exception protocol_version
    Error creating bean with name 'tomcatEmbeddedServletContainerFactory ' (or a BeanPostProcessor involved) returned null
    在Java8的foreach()中不能break,如果需要continue时,可以使用return
    Spring cloud 超时及重试配置【ribbon及其它http client】
    祸害阿里云宕机 3 小时的 IO HANG 究竟是个什么鬼?!
    Convertion of grey code and binary 格雷码和二进制数之间的转换
    stringstream 使用方法
    [LeetCode] 91. Decode Ways 解码方法
    QString, string, int, char* 之间相互转换
  • 原文地址:https://www.cnblogs.com/ike_li/p/13427544.html
Copyright © 2020-2023  润新知