Qt操作xml文件(增删改功能)
2023-09-11 14:16:38 时间
这个例子是在根据网上博客《Qt数据库(XML)》改写的一个操作XML的实现。
借鉴了很多里面的代码,大家可以结合上面的博客对照,相信你肯定会对XML的操作熟练起来。
我建立的是Qwidget项目,没有添加ui文件,输出内容都放在应用程序输出中(qDebug)。
XMLtest.pro文件代码:
- #-------------------------------------------------
- #
- # Project created by QtCreator 2012-08-15T15:56:54
- #
- #-------------------------------------------------
- QT += core gui xml
- TARGET = XMLtest
- TEMPLATE = app
- SOURCES += main.cpp\
- widget.cpp
- HEADERS += widget.h
widget.h文件代码:
- #ifndef WIDGET_H
- #define WIDGET_H
- #include <QtGui/QWidget>
- #include <QtCore>
- class Widget : public QWidget
- {
- Q_OBJECT
- public:
- Widget(QWidget *parent = 0);
- ~Widget();
- void read_xml(QString filename);
- void create_xml(QString filename);
- void add_xmlnode(QString filename,QString rmt_name,QString ipa,QString ipb);
- void do_xml(const QString opt,QString filename);
- private:
- };
- #endif // WIDGET_H
widget.cpp文件代码:
- #include "widget.h"
- #include "qfile.h"
- #include "qdebug.h"
- #include <QDomDocument>
- #include "qtextcodec.h"
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- {
- QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));
- QFile *file;
- QString filename = "config.xml";
- if(file->exists("config.xml"))
- {
- read_xml(filename);
- }
- else
- {
- create_xml(filename);
- }
- add_xmlnode(filename,"remote1","127.0.0.1","192.168.1.199");
- do_xml("update",filename);
- }
- Widget::~Widget()
- {
- }
- void Widget::do_xml(const QString opt,QString filename)
- {
- QFile file(filename);
- if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
- {
- qDebug() << "open for do erro";
- file.close();
- }
- QDomDocument doc;
- if(!doc.setContent(&file))
- {
- qDebug() << "setcontent for do error";
- file.close();
- }
- file.close();
- QDomNodeList lists = doc.elementsByTagName("remote");
- QDomElement ele = lists.at(1).toElement();
- if(ele.attribute(tr("id")) == "3")
- {
- if("delete" == opt || "update" == opt)
- {
- QDomElement root = doc.documentElement();
- if("delete" == opt)
- {
- root.removeChild(lists.at(1));
- qDebug() << "remove ok !";
- }
- else
- {
- QDomNodeList child=lists.at(1).childNodes();
- child.at(0).toElement().firstChild().setNodeValue("namechanged");
- child.at(1).toElement().firstChild().setNodeValue("ipachanged");
- child.at(2).toElement().firstChild().setNodeValue("ipbchanged");
- qDebug() << "modify ok !";
- }
- if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
- {
- qDebug() << "open for remove error!";
- }
- QTextStream out(&file);
- doc.save(out,4);
- file.close();
- }
- }
- }
- void Widget::add_xmlnode(QString filename,QString rmt_name, QString ipa, QString ipb)
- {
- QFile file(filename);
- if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
- qDebug()<<"open for add error..." ;
- }
- QDomDocument doc;
- QString errorStr;
- int errorLine;
- int errorColumn;
- if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn)) {
- qDebug()<<"add setcontent error..." ;
- file.close();
- }
- //QDomNode node = doc.firstChild();
- file.close();
- QDomElement root = doc.documentElement();
- if(root.isNull())
- {
- root = doc.createElement("ipconfig");
- }
- QDomElement element_root = doc.createElement(tr("remote"));
- QDomAttr attr_id = doc.createAttribute(tr("id"));
- QDomElement element_rmt = doc.createElement(tr("rmt_name"));
- QDomElement element_ipa = doc.createElement(tr("ipa"));
- QDomElement element_ipb = doc.createElement(tr("ipb"));
- QString str_id;
- if(root.lastChild().isNull())
- {
- str_id = "1";
- attr_id.setValue(str_id);
- }
- else
- {
- str_id = root.lastChild().toElement().attribute(tr("id"));
- int count = str_id.toInt()+1;
- attr_id.setValue(QString::number(count));
- }
- QDomText text;
- text =doc.createTextNode(rmt_name);
- element_rmt.appendChild(text);
- text = doc.createTextNode(ipa);
- element_ipa.appendChild(text);
- text = doc.createTextNode(ipb);
- element_ipb.appendChild(text);
- text.clear();
- element_root.appendChild(element_rmt);
- element_root.appendChild(element_ipa);
- element_root.appendChild(element_ipb);
- element_root.setAttributeNode(attr_id);
- root.appendChild(element_root);
- if(!file.open(QIODevice::WriteOnly|QIODevice::Append))
- qDebug() << "open for add error!";
- QTextStream out(&file);
- doc.save(out,4);
- file.close();
- }
- void Widget::read_xml(QString filename)
- {
- QFile file(filename);
- if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
- qDebug()<<"open for read error..." ;
- }
- QString errorStr;
- int errorLine;
- int errorColumn;
- QDomDocument doc;
- if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn)) {
- qDebug()<<"setcontent error..." ;
- file.close();
- }
- file.close();
- QDomElement root = doc.documentElement();
- if (root.tagName() != "ipconfig") {
- qDebug()<<"root.tagname != ipconfig..." ;
- }
- QDomNode node = root.firstChild();
- while(!node.isNull())
- {
- if(node.isElement())
- {
- QDomElement element = node.toElement();
- qDebug() << qPrintable(element.tagName())<<qPrintable(element.attribute("id"));
- QDomNodeList list = element.childNodes();
- for(int i = 0;i < list.count();i++)
- {
- QDomNode nodechild = list.at(i);
- if(nodechild.isElement())
- {
- qDebug() << " " << qPrintable(nodechild.toElement().tagName()) << qPrintable(nodechild.toElement().text());
- }
- }
- }
- node = node.nextSibling();
- }
- }
- void Widget::create_xml(QString filename)
- {
- QFile file(filename);
- file.open(QIODevice::ReadWrite);
- QDomDocument doc;
- QDomProcessingInstruction instruction;
- instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"GB2312\"");
- doc.appendChild(instruction);
- QDomElement root = doc.createElement("ipconfig");
- doc.appendChild(root);
- QDomText text = doc.createTextNode("");
- root.appendChild(text);
- QTextStream out(&file);
- doc.save(out,4);
- file.close();
- }
main.cpp文件代码:
- #include <QtGui/QApplication>
- #include "widget.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- return a.exec();
- }
XML文件结构:
- <?xml version='1.0' encoding='GB2312'?>
- <ipconfig>
- <remote id="1">
- <rmt_name>remote1</rmt_name>
- <ipa>127.0.0.1</ipa>
- <ipb>192.168.1.199</ipb>
- </remote>
- <remote id="3">
- <rmt_name>namechanged</rmt_name>
- <ipa>ipachanged</ipa>
- <ipb>ipbchanged</ipb>
- </remote>
- <remote id="4">
- <rmt_name>remote1</rmt_name>
- <ipa>127.0.0.1</ipa>
- <ipb>192.168.1.199</ipb>
- </remote>
- <remote id="5">
- <rmt_name>remote1</rmt_name>
- <ipa>127.0.0.1</ipa>
- <ipb>192.168.1.199</ipb>
- </remote>
- <remote id="6">
- <rmt_name>remote1</rmt_name>
- <ipa>127.0.0.1</ipa>
- <ipb>192.168.1.199</ipb>
- </remote>
- <remote id="7">
- <rmt_name>remote1</rmt_name>
- <ipa>127.0.0.1</ipa>
- <ipb>192.168.1.199</ipb>
- </remote>
- <remote id="8">
- <rmt_name>remote1</rmt_name>
- <ipa>127.0.0.1</ipa>
- <ipb>192.168.1.199</ipb>
- </remote>
- </ipconfig>
应用程序输出:
remote 1
rmt_name remote1
ipa 127.0.0.1
ipb 192.168.1.199
remote 3
rmt_name remote1
ipa 127.0.0.1
ipb 192.168.1.199
remote 4
rmt_name remote1
ipa 127.0.0.1
ipb 192.168.1.199
remote 5
rmt_name remote1
ipa 127.0.0.1
ipb 192.168.1.199
remote 6
rmt_name remote1
ipa 127.0.0.1
ipb 192.168.1.199
remote 7
rmt_name remote1
ipa 127.0.0.1
ipb 192.168.1.199
modify ok !
相关文章
- 工作路径下用CMD启动fluent(相对路径读写文件),QT中QProcess调用详解及实验
- Qt操作XML文件
- QT-Qt组件QTimer使用方法
- s3c2410上搭建QT/Embedded4.8.5开发环境(二)--安装arm平台qt库qt-everywhere-opensource-src-4.8.5
- QT实现启动画面
- golang encoding/xml 解析多个根节点的XML文件
- Qt音视频开发39-人脸识别在线版
- Qt编写水波进度条控件
- Qt编写地图综合应用20-多浏览器内核
- Qt系列文章009-Qt数据类型介绍
- Qt实现拖拽文件
- Qt QVariant的用法
- Qt xml文件常用的操作(读写,增删改查)
- Qt获取电脑磁盘容量
- Qt信号槽源码剖析
- 使用QT绘制一个多边形
- C++Qt开发——事件处理函数
- Qt基于FFmpeg解码本地视频生成H264文件并播放
- Qt实现透明按钮仅显示图标(亲测可用)
- Qt使用QJson生成json文件并保存
- qt 拷贝文件设置进度条
- 给.exe文件添加管理员权限(MinGW)--Qt
- Qt按钮透明后,长按出现黑色背景的解决方案
- 解决Ubuntu系统安装QT出错的问题:qt.qpa.plugin: Could not load the Qt platform plugin “xcb“ in ““ even though it
- Qt 获取文件夹下所有文件
- Qt creator 编译错误 :cannot find file .pro qt
- Qt开发中.pro文件中,CONFIG参数设置与版本号
- java载入XML文件并解析xml