Qt5 对xml文件常用的操作(读写,增删改查)
转自:https://blog.csdn.net/hpu11/article/details/80227093
项目配置
pro文件里面添加QT+=xml
include <QtXml>,也可以include <QDomDocument>
项目文件:
.pro 文件
QT += core xml QT -= gui TARGET = xmltest
CONFIG += console
CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp
主程序:
main.cpp
#include <QCoreApplication>
#include <QtXml> //也可以include <QDomDocument> //写xml
void WriteXml()
{
//打开或创建文件
QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
if(!file.open(QFile::WriteOnly|QFile::Truncate)) //可以用QIODevice,Truncate表示清空原来的内容
return; QDomDocument doc;
//写入xml头部
QDomProcessingInstruction instruction; //添加处理命令
instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instruction);
//添加根节点
QDomElement root=doc.createElement("library");
doc.appendChild(root);
//添加第一个子节点及其子元素
QDomElement book=doc.createElement("book");
book.setAttribute("id",); //方式一:创建属性 其中键值对的值可以是各种类型
QDomAttr time=doc.createAttribute("time"); //方式二:创建属性 值必须是字符串
time.setValue("2013/6/13");
book.setAttributeNode(time);
QDomElement title=doc.createElement("title"); //创建子元素
QDomText text; //设置括号标签中间的值
text=doc.createTextNode("C++ primer");
book.appendChild(title);
title.appendChild(text);
QDomElement author=doc.createElement("author"); //创建子元素
text=doc.createTextNode("Stanley Lippman");
author.appendChild(text);
book.appendChild(author);
root.appendChild(book); //添加第二个子节点及其子元素,部分变量只需重新赋值
book=doc.createElement("book");
book.setAttribute("id",);
time=doc.createAttribute("time");
time.setValue("2007/5/25");
book.setAttributeNode(time);
title=doc.createElement("title");
text=doc.createTextNode("Thinking in Java");
book.appendChild(title);
title.appendChild(text);
author=doc.createElement("author");
text=doc.createTextNode("Bruce Eckel");
author.appendChild(text);
book.appendChild(author);
root.appendChild(book); //输出到文件
QTextStream out_stream(&file);
doc.save(out_stream,); //缩进4格
file.close(); } //读xml
void ReadXml()
{
//打开或创建文件
QFile file("test.xml"); //相对路径、绝对路径、资源路径都行
if(!file.open(QFile::ReadOnly))
return; QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return;
}
file.close(); QDomElement root=doc.documentElement(); //返回根节点
qDebug()<<root.nodeName();
QDomNode node=root.firstChild(); //获得第一个子节点
while(!node.isNull()) //如果节点不空
{
if(node.isElement()) //如果节点是元素
{
QDomElement e=node.toElement(); //转换为元素,注意元素和节点是两个数据结构,其实差不多
qDebug()<<e.tagName()<<" "<<e.attribute("id")<<" "<<e.attribute("time"); //打印键值对,tagName和nodeName是一个东西
QDomNodeList list=e.childNodes();
for(int i=;i<list.count();i++) //遍历子元素,count和size都可以用,可用于标签数计数
{
QDomNode n=list.at(i);
if(node.isElement())
qDebug()<<n.nodeName()<<":"<<n.toElement().text();
}
}
node=node.nextSibling(); //下一个兄弟节点,nextSiblingElement()是下一个兄弟元素,都差不多
} } //增加xml内容
void AddXml()
{
//打开文件
QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
if(!file.open(QFile::ReadOnly))
return; //增加一个一级子节点以及元素
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return;
}
file.close(); QDomElement root=doc.documentElement();
QDomElement book=doc.createElement("book");
book.setAttribute("id",);
book.setAttribute("time","1813/1/27");
QDomElement title=doc.createElement("title");
QDomText text;
text=doc.createTextNode("Pride and Prejudice");
title.appendChild(text);
book.appendChild(title);
QDomElement author=doc.createElement("author");
text=doc.createTextNode("Jane Austen");
author.appendChild(text);
book.appendChild(author);
root.appendChild(book); if(!file.open(QFile::WriteOnly|QFile::Truncate)) //先读进来,再重写,如果不用truncate就是在后面追加内容,就无效了
return;
//输出到文件
QTextStream out_stream(&file);
doc.save(out_stream,); //缩进4格
file.close();
} //删减xml内容
void RemoveXml()
{
//打开文件
QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
if(!file.open(QFile::ReadOnly))
return; //删除一个一级子节点及其元素,外层节点删除内层节点于此相同
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return;
}
file.close(); //一定要记得关掉啊,不然无法完成操作 QDomElement root=doc.documentElement();
QDomNodeList list=doc.elementsByTagName("book"); //由标签名定位
for(int i=;i<list.count();i++)
{
QDomElement e=list.at(i).toElement();
if(e.attribute("time")=="2007/5/25") //以属性名定位,类似于hash的方式,warning:这里仅仅删除一个节点,其实可以加个break
root.removeChild(list.at(i));
} if(!file.open(QFile::WriteOnly|QFile::Truncate))
return;
//输出到文件
QTextStream out_stream(&file);
doc.save(out_stream,); //缩进4格
file.close();
} //更新xml内容
void UpdateXml()
{
//打开文件
QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
if(!file.open(QFile::ReadOnly))
return; //更新一个标签项,如果知道xml的结构,直接定位到那个标签上定点更新
//或者用遍历的方法去匹配tagname或者attribut,value来更新
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return;
}
file.close(); QDomElement root=doc.documentElement();
QDomNodeList list=root.elementsByTagName("book");
QDomNode node=list.at(list.size()-).firstChild(); //定位到第三个一级子节点的子元素
QDomNode oldnode=node.firstChild(); //标签之间的内容作为节点的子节点出现,当前是Pride and Projudice
node.firstChild().setNodeValue("Emma");
QDomNode newnode=node.firstChild();
node.replaceChild(newnode,oldnode); if(!file.open(QFile::WriteOnly|QFile::Truncate))
return;
//输出到文件
QTextStream out_stream(&file);
doc.save(out_stream,); //缩进4格
file.close();
} int main(int argc, char *argv[])
{ qDebug()<<"write xml to file...";
WriteXml();
qDebug()<<"read xml to display...";
ReadXml();
qDebug()<<"add contents to xml...";
AddXml();
qDebug()<<"remove contents from xml...";
RemoveXml();
qDebug()<<"update contents to xml...";
UpdateXml();
return ; }
写xml
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="1" time="2013/6/13">
<title>C++ primer</title>
<author>Stanley Lippman</author>
</book>
<book id="2" time="2007/5/25">
<title>Thinking in Java</title>
<author>Bruce Eckel</author>
</book>
</library>
增加xml
<?xml version='1.0' encoding='UTF-8'?>
<library>
<book time="2013/6/13" id="">
<title>C++ primer</title>
<author>Stanley Lippman</author>
</book>
<book time="2007/5/25" id="">
<title>Thinking in Java</title>
<author>Bruce Eckel</author>
</book>
<book time="1813/1/27" id="">
<title>Pride and Prejudice</title>
<author>Jane Austen</author>
</book>
</library>
删除xml
<?xml version='1.0' encoding='UTF-8'?>
<library>
<book time="2013/6/13" id="">
<title>C++ primer</title>
<author>Stanley Lippman</author>
</book>
<book time="2007/5/25" id="">
<title>Thinking in Java</title>
<author>Bruce Eckel</author>
</book>
<book time="1813/1/27" id="">
<title>Pride and Prejudice</title>
<author>Jane Austen</author>
</book>
</library>
更新xml
<?xml version='1.0' encoding='UTF-8'?>
<library>
<book id="" time="2013/6/13">
<title>C++ primer</title>
<author>Stanley Lippman</author>
</book>
<book id="" time="1813/1/27">
<title>Emma</title>
<author>Jane Austen</author>
</book>
</library>
Qt5 对xml文件常用的操作(读写,增删改查)的更多相关文章
- 对xml文件的sax解析(增删改查)之一
crud(增删改查): c:creat r:retrieve u:update d:delete 以下笔记来自于韩顺平老师的讲解. 现在是用java来操作. 第一步:新建java工程.file-new ...
- 对xml文件的sax解析(增删改查)之二
先上代码: package com.saxparsetest; //the filename of this file is :saxparse.java import javax.xml.parse ...
- 使用DOM进行xml文档的crud(增删改查)操作<操作详解>
很多朋友对DOM有感冒,这里我花了一些时间写了一个小小的教程,这个能看懂,会操作了,我相信基于DOM的其它API(如JDOM,DOM4J等)一般不会有什么问题. 后附java代码,也可以下载(可点击这 ...
- js操作indexedDB增删改查示例
js操作indexedDB增删改查示例 if ('indexedDB' in window) { // 如果数据库不存在则创建,如果存在但是version更大,会自动升级不会复制原来的版本 var r ...
- MySQL数据分析(16)— 数据操作之增删改查
前面我们说学习MySQL要从三个层面,四大逻辑来学,三个层面就是库层面,表层面和数据层面对吧,数据库里放数据表,表里放数据是吧,大家可以回忆PPT中jacky的这图,我们已经学完了库层面和表层面,从本 ...
- Redis简单的数据操作(增删改查)
#Redis简单的数据操作(增删改查): 字符串类型 string 1. 存储: set key value 127.0.0.1:6379> set username zhangsan OK 2 ...
- 02 . Mysql基础操作及增删改查
SQL简介 SQL(Structured Query Language 即结构化查询语言) SQL语言主要用于存取数据.查询数据.更新数据和管理关系数据库系统,SQL语言由IBM开发. SQL语句四大 ...
- Django-Model操作数据库(增删改查、连表结构)
一.数据库操作 1.创建model表 基本结构 1 2 3 4 5 6 from django.db import models class userinfo(models.M ...
- 【DRP】-Dao层常用功能代码:增删改查
本系列博客内容为:做DRP系统中Dao层常用功能. 该项目采用MVC架构 C(Controller)控制器,主要职责;1.取得表单参数:2.调用业务逻辑:3.转向页面 M(Model)模型,主要职责: ...
随机推荐
- sql server,mysql 和navicat for mysql的区别
一.定义 sql server 应该指的是sqlserver数据库,包含数据库管理系统等. navicat for sql server只是一个sqlserver的第三方的开发工具,管理工具. 二.开 ...
- 10个你不得不知的WEB移动端开发的兼容问题
1.IOS下input设置type=button属性disabled设置true,会出现样式文字和背景异常问题,使用opacity=1来解决 2.一些情况下对非可点击元素如(label,span)监听 ...
- codevs 2038 香甜的黄油x+luogu P1828 x
题目描述 Description 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油 ...
- [ethereum源码分析](2) ethereum基础知识
前言 上一章我们介绍了如何搭建ethereum的debug环境.为了更深入的了解ethereum,我们需要了解一些ethereum的相关的知识,本章我们将介绍这些知识. ethereum相关知识 在学 ...
- 30 年前的圣诞节,Python 序章被谱写
1989 年圣诞节期间,已经从阿姆斯特丹大学(University of Amsterdam)获得数学和计算机硕士学位的 Guido van Rossum,为了打发圣诞节的无趣,决心开发一个新语言解释 ...
- 大数据笔记(三)——Hadoop2.0的安装与配置
一.Hadoop安装部署的预备条件 准备:1.安装Linux和JDK. 安装JDK 解压:tar -zxvf jdk-8u144-linux-x64.tar.gz -C ~/training/ 设置环 ...
- Linux下开启FTP服务
一.配置步骤 1.安装vsftp 使用yum命令安装vsftp #yum install vsftpd -y 2.添加ftp帐号和目录 先确定nologin的位置,通常在/usr/sbin/nolog ...
- 源码编译安装Apache/2.4.37-------踩了无数坑,重装了十几次服务器才会的,不容易啊!
1.先进入/usr/local/中创建三个文件夹 apr apr-util apache cd /usr/local目录 mkdir apr mkdir apr-util mkdir apache 2 ...
- ORACLE异机增量备份恢复
PROD异机增量备份恢复验证实施文档 准备工作:source 源库:PROD数据库备份策略:周日0级RMAN备份,周一至周六1级差异增量备份0 4 * * 0 /data/rmanlev0.sh &g ...
- 【洛谷P1069 细胞分裂】
题目链接 首先,光看题就觉得它很扯淡(你哪里来这么多的钱来买试管) 根据某位已经ak过ioi的名为ych的神仙说(一看就是数学题,一看就需要因式分解,emm,我果然没有发现美的眼睛qwq) 那么我们就 ...