Qt操作xml文件(增删改功能)
这个例子是在根据网上博客《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 !
Qt操作xml文件(增删改功能)的更多相关文章
- java实现xml文件增删改查
java一次删除xml多个节点: 方案1.你直接改动了nodeList,这一般在做循环时是不同意直接这么做的. 你能够尝试在遍历一个list时,在循环体同一时候删除list里的内容,你会得到一个异常. ...
- flex 操作xml 实现增删改查 .
一 在介绍Flex中操作XML之前,首先简单介绍下XML中的基本术语. 元素:XML中拥有开始标签和结束标签的这一块称为“元素” 节点:把XML元素与文本结合起来统称为节点 根节点:位于整 ...
- .NET XML文件增删改查
查询 采用的是DataSet 的 ReadXML方法. DataSet ds = new System.Data.DataSet(); ds.ReadXml("bdc.xml"); ...
- Asp.Net 操作XML文件的增删改查 利用GridView
不废话,直接上如何利用Asp.NET操作XML文件,并对其属性进行修改,刚开始的时候,是打算使用JS来控制生成XML文件的,但是最后却是无法创建文件,读取文件则没有使用了 index.aspx 文件 ...
- Qt之QDomDocument操作xml文件-模拟ini文件存储
一.背景 不得不说Qt是一个很强大的类库,不管是做项目还是做产品,Qt自身封装的东西就已经非常全面了,我们今天的这篇文章就是模拟了Qt读写ini文件的一个操作,当然是由于一些外力原因,我们决定自己来完 ...
- java中XML操作:xml与string互转、读取XML文档节点及对XML节点增删改查
一.XML和String互转: 使用dom4j程式变得很简单 //字符串转XML String xmlStr = \"......\"; Document document = D ...
- JAVA中通过Jaxp操作XML文件基础
Java中有多种方式操作XML文件,目前讲一讲以SUN公司提供的DocumentBuilderFactory工厂类对象操作XML. 使用XML基本操作就是需要CRUD(增删改查),那么首先通过一个查询 ...
- 使用idea对XML的增删改查
XML:是一种可扩展标记性的语言,与java语言无关,它可以自定义标签. 1.首先需要到导入Dom4j架包,与自己所时候的ide关联 2.编写自己的xml文件,入上图所示(里面的所有元素及元素中的属性 ...
- C#基础知识---Linq操作XML文件
概述 Linq也就是Language Integrated Query的缩写,即语言集成查询,是微软在.Net 3.5中提出的一项新技术. Linq主要包含4个组件---Linq to Objects ...
随机推荐
- SEPM安装完之后的一些细节之处
1. 若SEPM与GUP为同一台主机,则必须在其上也安装SEP, 否则其他客户端无法更新. 2. 先指定GUP,然后指派策略 3. Latest on Manager可以通过离线jdb文件进行 ...
- 【液晶模块系列基础视频】5.2.X-GUI字体驱动2
============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...
- 校内OJ 1128 词链(link)(Trie+DFS)
1128: 词链(link) 时间限制: 1 Sec 内存限制: 64 MB 提交: 23 解决: 7 [提交][状态][讨论版] 题目描述 给定一个仅包含小写字母的英文单词表,其中每个单词最多包 ...
- x86_64编译JPEG遇到Invalid configuration `x86_64-unknown-linux-gnu'
把 /usr/share/libtool/config/config.guess 覆盖到相关软件自带的config.guess 把 /usr/share/libtool/config/config ...
- 提取安卓手机的recovery
一直都是从网上下载的recovery文件安装到手机.至于这个小小的recovery到底是什么全然不知.能不能自己做一个recovery呢?因为功能比较多的clockworkmod(简称cmw)的官网上 ...
- PHP+jQuery 注册模块的改进之三:使用 Smarty3
Smarty3.1X( 最新版本 3.1.19) 比起Smarty2.x修改了不少特性.我把这个模块使用Smarty3.1.18 ( 下载地址http://www.smarty.net/files/S ...
- How Browsers Work: Behind the scenes of modern web browsers
http://www.html5rocks.com/en/tutorials/internals/howbrowserswork/#Parser_Lexer_combination Grammars ...
- 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】
树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...
- 新建android项目报错,代码中找不到错误
通过网上资料的引导,做以下操作: 1.进入C:\Documents and Settings\Administrator\.android 删除路径下的debug.keystore及 ddms.cfg ...
- FW:: ehcache memcache redis 三大缓存男高音
最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考! Ehcache 在java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS ...