Qt操作xml文件(增删改功能)
来源:互联网 发布:麻瓜编程python 编辑:程序博客网 时间:2024/04/29 15:50
这个例子是在根据网上博客《Qt数据库(XML)》改写的一个操作XML的实现。
借鉴了很多里面的代码,大家可以结合上面的博客对照,相信你肯定会对XML的操作熟练起来。
我建立的是Qwidget项目,没有添加ui文件,输出内容都放在应用程序输出中(qDebug)。
XMLtest.pro文件代码:
#-------------------------------------------------## Project created by QtCreator 2012-08-15T15:56:54##-------------------------------------------------QT += core gui xmlTARGET = XMLtestTEMPLATE = appSOURCES += main.cpp\ widget.cppHEADERS += 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文件(增删改功能)
- Qt操作xml文件(增删改功能)
- Qt操作xml文件(增删改功能)
- Qt xml增删改
- Java操作XML文件大合集(增删改查)
- Java操作XML文件大合集(增删改查)
- Java操作XML文件大合集(增删改查)
- XML文件操作,新建,增删改查
- 增删改Xml文件
- .NET操作XML文件:XML文件节点的增删改
- xml文件(内容)的增删改
- 代码操作XML(增删改)
- dom对xml文件增删查改操作
- C#编程之XML文件对象增删改操作
- C# XML文件的一些操作,增删改查
- 解析XML文件并且进行增删改查操作
- java代码对xml文件进行增删改查操作
- C# 简单操作XML文档(增删查改)
- ubuntu如何查看MAC地址
- android代码混淆- 原来如此简单
- hdu1385Minimum Transport Cost-floyd+打印路径
- Java 线程池的原理与实现
- Java 线程池学习
- Qt操作xml文件(增删改功能)
- 全局事务与本地事务的区别应用
- Swing进度条
- Java多线程初学者指南:线程的生命周期
- 1165 Eddy's research II
- iphone 的io操作详解
- 评分插件
- android之动画开发
- 利用ES文件浏览器访问电脑上的文件