1. 程式人生 > >QT中XML增刪改查,不會有重複beta

QT中XML增刪改查,不會有重複beta

最近在做QT專案,要把一些配置內容寫入xml中,網上沒有一個可以概括xml增刪改查的內容,現在寫一篇,包含完整的增刪改查,測試通過,具體程式碼如下:

其中的測試程式碼見mainwindows.cpp裡面,裡面有建立xml檔案,增加資料,刪除資料,修改資料,查詢資料,其中沒有重複內容。刪除的時候也把相同的標籤全部刪掉了。

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    XMLParser *xmlparser = new XMLParser("D:/","test.xml");

    QFile *file;
    //QString  filename = "D:/test.xml";
    if(!file->exists("D:/test.xml"))  {
        xmlparser->create_xml("D:/","test.xml");

    }
    bool ret = xmlparser->add_element("D:/","test.xml","password","12345");

    QString password = xmlparser->get_element("D:/","test.xml","password");

    qDebug()<<"password is "<<password;

    bool ret01 = xmlparser->update_element("D:/","test.xml","password","342345");


    bool ret02 = xmlparser->delete_element("D:/","test.xml","password","1212342345");
    qDebug()<<"ret02 is "<<ret02;

    bool ret03 = xmlparser->delete_element("D:/","test.xml","password","342345");
    qDebug()<<"ret03 is "<<ret03;

//    QString path = xmlparser->get_savepath();

//    if(NULL==path||path==""){
//        QString fileName = QFileDialog::getExistingDirectory(this, tr("請選擇儲存目錄"),
//                                                          "",
//                                                       QFileDialog::ShowDirsOnly
//                                                          | QFileDialog::DontResolveSymlinks);
//        qDebug()<<"fileName is "<<fileName;
//        bool ret = xmlparser->set_savepath(fileName);
//        qDebug()<<"ret is "<<ret;
//    }else{
//        qDebug()<<"path is "<<path;
//    }


}

MainWindow::~MainWindow()
{

    delete ui;
}

xmlparser.cpp
#include "xmlparser.h"

#define __XMLPARSER_DEBUG__

XMLParser::XMLParser(QObject *parent) : QObject(parent)
{

    configPath = dir.currentPath()+"/setting.xml";
    file=new QFile(configPath);

}


XMLParser::XMLParser(QString filepath ,QString filename,QObject *parent) : QObject(parent)
{
    this->filepath = filepath;
    this->filename=filename;

}

QString XMLParser::get_savepath(){

    QDomDocument dom;

    if(file->open(QIODevice::ReadOnly))
    {
        dom.setContent(file);
    }else{
        return NULL;
    }
    QDomNodeList save_path=dom.elementsByTagName("save_path");

    int elementnum = save_path.count();
    QString savepath = save_path.item(0).toElement().text();

    file->close();
    if(NULL!=savepath&&savepath!=""){
        return savepath;
    }else{
        return NULL;
    }

}

bool XMLParser::set_savepath(QString path){

    if(NULL==path||path==""){
        return false;
    }
    if(!file->open(QIODevice::WriteOnly|QIODevice::ReadOnly))
    {
        return false;
    }

    QDomDocument doc;
    QDomText text;
    QDomElement element;
    QDomProcessingInstruction instruction;
    instruction=doc.createProcessingInstruction("xml","version=\'1.0\'");
    doc.appendChild(instruction);
    QDomElement root=doc.createElement("root");
    doc.appendChild(root);
    QDomElement general=doc.createElement("general");
    root.appendChild(general);

    element=doc.createElement("save_path");
    text=doc.createTextNode(path);
    element.appendChild(text);
    general.appendChild(element);

    QTextStream out(file);
    doc.save(out,4);
    file->close();

    return true;
}


QString XMLParser::get_element(QString filepath ,QString filename,QString elementstr){
    if(NULL==filepath||NULL==filename||NULL==elementstr){
        return NULL;
    }
    QString Path = filepath+filename;
    //QString Path = filepath.endsWith("/")?filepath+filename:filepath.mid(0,filepath.length()-1);+filename;
    QFile myfile(Path);
    QDomDocument dom;

    if(myfile.open(QIODevice::ReadOnly))
    {
        QString errorStr;
        int errorLine;
        int errorColumn;

        //QDomDocument doc;
        if (!dom.setContent(&myfile, false, &errorStr, &errorLine, &errorColumn)) {
#ifdef __XMLPARSER_DEBUG__
            qDebug()<<"setcontent error..."<<" errorStr is "<<errorStr<<" errorLine is "<<errorLine<<" errorColumn is "<<errorColumn ;
#endif
            myfile.close();
        }
        myfile.close();

        //dom.setContent(myfile);
    }else{
#ifdef __XMLPARSER_DEBUG__
        qDebug()<<"open xml read error..." ;
#endif
        return NULL;
    }

    QDomElement root = dom.documentElement();
    if (root.tagName() != "root") {
#ifdef __XMLPARSER_DEBUG__
        qDebug()<<"root.tagname != root..." ;
#endif
    }

    QDomNode node = root.firstChild();
    while(!node.isNull())
    {
        if(node.isElement()){
            QDomElement element = node.toElement();
#ifdef __XMLPARSER_DEBUG__
            qDebug()<<"tagName is "<<element.tagName()<<" element.text is "<<element.text();
#endif

            if(element.tagName()==elementstr){
                return (element.text().length()>0&&NULL!=element.text())?element.text():NULL;
            }

        }
        node = node.nextSibling();
    }
    return NULL;



    //    QDomNodeList save_path=dom.elementsByTagName(element);

    //    int elementnum = save_path.count();
    //    QString savepath = save_path.item(0).toElement().text();

    //    myfile->close();
    //    if(NULL!=savepath&&savepath!=""){
    //        return savepath;
    //    }else{
    //        return NULL;
    //    }

}

bool XMLParser::add_element(QString filepath ,QString filename,QString element,QString value){

#ifdef __XMLPARSER_DEBUG__
    qDebug()<<"in add_element";
#endif
    if(NULL==filepath||NULL==filename||NULL==element||NULL==value){
        return NULL;
    }
    //QString Path = filepath.endsWith("/")?filepath+filename:filepath.mid(0,filepath.length()-1);+filename;

    QString Path = filepath+filename;

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

    QDomDocument doc;
    QString errorStr;
    int errorLine;
    int errorColumn;

    if (!doc.setContent(&myfile, false, &errorStr, &errorLine, &errorColumn)) {
#ifdef __XMLPARSER_DEBUG__
        qDebug()<<"setcontent error..."<<" errorStr is "<<errorStr<<" errorLine is "<<errorLine<<" errorColumn is "<<errorColumn ;
#endif
        myfile.close();
    }
    myfile.close();
    QDomElement root = doc.documentElement();
    if(root.isNull())
    {
        root = doc.createElement("root");
    }


    bool if_exist = false;
    QDomNode node = root.firstChild();
    while(!node.isNull())
    {
        if(node.isElement()){
            QDomElement local_element = node.toElement();
#ifdef __XMLPARSER_DEBUG__
            qDebug()<<"tagName is "<<local_element.tagName()<<" element.text is "<<local_element.text();

#endif
            if(local_element.tagName()==element){
                if_exist = true;
                break;
            }

        }
        node = node.nextSibling();
    }

    if(!if_exist){
        QDomElement element_my = doc.createElement(element);
        QDomText text;
        text =doc.createTextNode(value);
        element_my.appendChild(text);
        root.appendChild(element_my);
    }
#ifdef __XMLPARSER_DEBUG__
    qDebug()<<"element is "<<element;
    qDebug()<<"value is "<<value;
#endif
    if(!myfile.open(QIODevice::WriteOnly)){
#ifdef __XMLPARSER_DEBUG__
        qDebug() << "open for add error!";
#endif
    }
    QTextStream out(&myfile);
    doc.save(out,4);
    myfile.close();

    return true;


}

void XMLParser::create_xml(QString filepath ,QString filename){
#ifdef __XMLPARSER_DEBUG__
    qDebug()<<"in create_xml";
#endif
    if(NULL==filepath||NULL==filename){
        return;
    }
    QString Path = filepath+filename;
    //QString Path = filepath.endsWith("/")?filepath+filename:filepath.mid(0,filepath.length()-1);+filename;
    QFile myfile(Path);
    myfile.open(QIODevice::ReadWrite);

    QDomDocument doc;
    QDomProcessingInstruction instruction;
    instruction = doc.createProcessingInstruction("xml","version=\'1.0\'");
    doc.appendChild(instruction);
    QDomElement root = doc.createElement("root");

    doc.appendChild(root);
    QDomText text = doc.createTextNode("");
    root.appendChild(text);
    QTextStream out(&myfile);
    doc.save(out,4);
    myfile.close();
    return;
}

bool XMLParser::update_element(QString filepath ,QString filename,QString element,QString value){
    bool if_sucess = false;
    if(NULL==filepath||NULL==filename||NULL==element||NULL==value){
        return false;
    }

    QString Path = filepath+filename;

    QFile myfile(Path);
    QDomDocument doc;
    QString errorStr;
    int errorLine;
    int errorColumn;

    if (!doc.setContent(&myfile, false, &errorStr, &errorLine, &errorColumn)) {
#ifdef __XMLPARSER_DEBUG__
        qDebug()<<"setcontent error..."<<" errorStr is "<<errorStr<<" errorLine is "<<errorLine<<" errorColumn is "<<errorColumn ;
#endif
        myfile.close();
    }

    myfile.close();

    QDomElement root = doc.documentElement();

    QDomNodeList lists = doc.elementsByTagName(element);

    //新建節點
    QDomElement newNodeTag = doc.createElement(element);
    QDomText newNodeText = doc.createTextNode(value);
    newNodeTag.appendChild(newNodeText);

    int num = 0;
    for(num = 0;num<lists.size();num++){
        QDomElement ele = lists.at(num).toElement();
#ifdef __XMLPARSER_DEBUG__
        qDebug() << "element text is "<<ele.text();

#endif
        //把老的節點替換成新建節點
        root.replaceChild(newNodeTag,ele);
        if_sucess = true;
#ifdef __XMLPARSER_DEBUG__
        qDebug() << "modify ok ! value is "<<value;
#endif
    }

    if(!myfile.open(QIODevice::WriteOnly | QIODevice::Text))
    {
#ifdef __XMLPARSER_DEBUG__
        qDebug() << "open for remove error!";
#endif
    }
    QTextStream out(&myfile);
    doc.save(out,4);
    myfile.close();
    if(if_sucess){
        return if_sucess;
    }else{
        return if_sucess;
    }
    //    return true;

}

bool XMLParser::delete_element(QString filepath ,QString filename,QString element,QString value){
    if(NULL==filepath||NULL==filename||NULL==element||NULL==value){
        return false;
    }


    QString Path = filepath+filename;
    bool if_sucess = false;
    QFile myfile(Path);
    QDomDocument doc;
    QString errorStr;
    int errorLine;
    int errorColumn;

    if (!doc.setContent(&myfile, false, &errorStr, &errorLine, &errorColumn)) {
#ifdef __XMLPARSER_DEBUG__
        qDebug()<<"setcontent error..."<<" errorStr is "<<errorStr<<" errorLine is "<<errorLine<<" errorColumn is "<<errorColumn ;
#endif
        myfile.close();
    }

    myfile.close();

    QDomElement root = doc.documentElement();

    QDomNodeList lists = doc.elementsByTagName(element);

    //新建節點
    QDomElement newNodeTag = doc.createElement(element);
    QDomText newNodeText = doc.createTextNode(value);
    newNodeTag.appendChild(newNodeText);

    int num = 0;
    for(num = 0;num<lists.size();num++){
        QDomElement ele = lists.at(num).toElement();
#ifdef __XMLPARSER_DEBUG__
        qDebug() << "element text is "<<ele.text();
#endif

        //刪除節點
        if(ele.text()==value){
            root.removeChild(ele);
            if_sucess = true;
        }
#ifdef __XMLPARSER_DEBUG__
        qDebug() << "modify ok ! value is "<<value;
#endif
    }

    if(!myfile.open(QIODevice::WriteOnly | QIODevice::Text))
    {
#ifdef __XMLPARSER_DEBUG__
        qDebug() << "open for remove error!";
#endif
    }
    QTextStream out(&myfile);
    doc.save(out,4);
    myfile.close();
    if(if_sucess){
        return if_sucess;
    }else{
        return if_sucess;
    }

}


XMLParser::~XMLParser(){
    if(NULL!=file){
        file->close();
        file=NULL;
    }
}

列印的結果是
in add_element
element is  "password"
value is  "12345"
tagName is  "password"  element.text is  "12345"
password is  "12345"
element text is  "12345"
modify ok ! value is  "342345"
element text is  "342345"
modify ok ! value is  "1212342345"
ret02 is  false
element text is  "342345"
modify ok ! value is  "342345"
ret03 is  true

程式碼已經放到github上,另外,也放到了csdn資源上

https://github.com/buptis073114/XMLParseExample