zoukankan      html  css  js  c++  java
  • 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();
    }
  • 相关阅读:
    安全检测点的一些梳理——待长期整理
    Tor真的匿名和安全吗?——如果是http数据,则在出口节点容易被嗅探明文流量,这就是根本问题
    prefixspan是挖掘频繁子序列,子序列不一定是连续的,当心!!!
    spark mllib prefixspan demo
    spark 2.4 java8 hello world
    有效的括号序列——算法面试刷题4(for google),考察stack
    相似的RGB颜色——算法面试刷题3(for google),考察二分
    回文的范围——算法面试刷题2(for google),考察前缀和
    最长绝对文件路径——算法面试刷题1(google),字符串处理,使用tree遍历dfs类似思路
    比较全面的gdb调试命令
  • 原文地址:https://www.cnblogs.com/ike_li/p/13427544.html
Copyright © 2011-2022 走看看