2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > [QT操作XML]QT读写XML文件 QT修改XML文件

[QT操作XML]QT读写XML文件 QT修改XML文件

时间:2019-06-15 09:10:35

相关推荐

[QT操作XML]QT读写XML文件 QT修改XML文件

[QT操作XML]QT读写XML文件

XML简介QT操作XML,写入、读取、修改XML效果演示

XML简介

概念:Extensible Markup Language 可扩展标记语言(可扩展:标签都是自定义的)

功能:存储数据(1. 配置文件、2. 在网络中传输)

XML基本语法:

1. xml文档的后缀名 .xml

2. xml第一行必须定义为文档声明

3. xml文档中有且仅有一个根标签

4. 属性值必须使用引号(单双都可)引起来

5. 标签必须正确关闭

6. xml标签名称区分大小写

文档声明格式:<?xml version='1.0' encoding='UTF-8'?>

version:版本号,必须的属性

encoding:编码方式,告知解析引擎当前文档使用的字符集编码方式,默认值:ISO-8859-1

standalone:是否独立(取值,yes:不依赖其他文件;no:依赖其他文件)

QT操作XML,写入、读取、修改

pro文件增加xml模块:QT += xml

包含头文件:#include

//写入XMLvoid MainWindow::writeXML(){QFile file("test.xml");//打开或新建xml文件if(!file.open(QIODevice::WriteOnly|QIODevice::Truncate))//Truncate表示清空原来的内容{QMessageBox::warning(this,"错误","文件打开失败");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",1);//方法1,创建属性,键值对可以是各种类型QDomAttr time = doc.createAttribute("time");//方法2,创建属性,值必须是字符串time.setValue("/6/3");book.setAttributeNode(time);QDomElement title = doc.createElement("title");//创建子元素QDomText text = doc.createTextNode("C++ primer");//设置括号标签中间的值book.appendChild(title);title.appendChild(text);QDomElement author = doc.createElement("author");//创建子元素text = doc.createTextNode("Stanley B.Lippman");author.appendChild(text);book.appendChild(author);root.appendChild(book);//添加第二个子节点,部分变量只需重新赋值book=doc.createElement("book");book.setAttribute("id",2);time = doc.createAttribute("time");time.setValue("/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);//缩进4格file.close();}//读XML文件void MainWindow::readXML(){QFile file("test.xml");if(!file.open(QIODevice::ReadOnly)){QMessageBox::warning(this,"错误","读XML,文件打开失败");return;}QDomDocument doc;if(!doc.setContent(&file))//从字节数组中解析XML文档,并将其设置为文档的内容{file.close();return;}file.close();QDomElement root = doc.documentElement();//返回根节点qDebug()<<"["<<__FILE__<<"]"<<__LINE__<<__FUNCTION__<<"root.nodeName "<<root.nodeName();//打印根节点QDomNode node = root.firstChild();//获得第一个子节点while(!node.isNull())//如果节点不为空{if(node.isElement())//如果节点是元素{QDomElement e= node.toElement();//节点转换为元素//打印键值对,tagName和nodeName相同qDebug() << e.tagName()<< " " <<e.attribute("id")<<" "<<e.attribute("time");QDomNodeList list = e.childNodes();//子节点列表for(int i=0;i<list.count();i++)//遍历子节点{QDomNode n = list.at(i);if(n.isElement())qDebug() << n.nodeName()<<":"<<n.toElement().text();}}node=node.nextSibling();//下一个兄弟节点}}//增加XML内容//先把文件读进来,再重写void MainWindow::addXML(){QFile file("test.xml");if(!file.open(QIODevice::ReadOnly)){QMessageBox::warning(this,"错误","增加XML,文件打开失败1");return;}QDomDocument doc;if(!doc.setContent(&file))//从字节数组中解析XML文档,并将其设置为文档的内容{file.close();return;}file.close();QDomElement root=doc.documentElement();QDomElement book=doc.createElement("book");book.setAttribute("id",3);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就是在后面追加内容,就无效了{QMessageBox::warning(this,"错误","增加XML,文件打开失败2");return;}QTextStream out_stream(&file);doc.save(out_stream,4);file.close();}//删减XML内容void MainWindow::removeXML(){QFile file("test.xml");if(!file.open(QIODevice::ReadOnly)){QMessageBox::warning(this,"错误","增加XML,文件打开失败1");return;}QDomDocument doc;if(!doc.setContent(&file))//从字节数组中解析XML文档,并将其设置为文档的内容{file.close();return;}file.close();QDomElement root=doc.documentElement();QDomNodeList list = doc.elementsByTagName("book");//指定名称的节点列表for(int i=0;i<list.count();i++){QDomElement e = list.at(i).toElement();if(e.attribute("time")=="/5/25")root.removeChild(list.at(i));}if(!file.open(QFile::WriteOnly|QFile::Truncate))//重写文件,如果不用truncate就是在后面追加内容,就无效了{QMessageBox::warning(this,"错误","删减XML内容,文件打开失败");return;}QTextStream out_stream(&file);doc.save(out_stream,4);file.close();}//更新XML内容//如果了解XML结构,可以直接定位到指定标签上更新//或者用遍历的方法去匹配tagname或者attribut,value来更新void MainWindow::updateXML(){QFile file("test.xml");if(!file.open(QIODevice::ReadOnly)){QMessageBox::warning(this,"错误","更新XML,文件打开失败1");return;}QDomDocument doc;if(!doc.setContent(&file))//从字节数组中解析XML文档,并将其设置为文档的内容{file.close();return;}file.close();QDomElement root = doc.documentElement();//获得根节点QDomNodeList list = root.elementsByTagName("book");//指定名称的节点列表QDomNode node = list.at(list.count()-1).firstChild();//定位到第三个一级子节点的子元素QDomNode oldNode = node.firstChild();//标签之间的内容作为节点的子节点出现,当前是Pride and ProjudiceoldNode.setNodeValue("dalao");//修改元素内容// node.firstChild().setNodeValue("diannao");// QDomNode newNode = node.firstChild();// node.replaceChild(newNode,oldNode);if(!file.open(QFile::WriteOnly|QFile::Truncate))//重写文件,如果不用truncate就是在后面追加内容,就无效了{QMessageBox::warning(this,"错误","更新XML内容,文件打开失败2");return;}QTextStream out_stream(&file);doc.save(out_stream,4);file.close();}

XML效果演示

<?xml version='1.0' encoding='UTF-8'?><library><book id="1" time="/6/3"><title>C++ primer</title><author>Stanley B.Lippman</author></book><book id="2" time="/5/25"><title>Thinking in Java</title><author>Bruce Eckel</author></book><book id="3" time="1813/1/27"><title>diannao</title><author>Jane Austen</author></book></library>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。
相关阅读
Qt 读写XML文件

Qt 读写XML文件

2020-06-07

Qt基础篇:Qt读写XML文件

Qt基础篇:Qt读写XML文件

2019-10-26

QT之xml文件读写

QT之xml文件读写

2019-06-06

Qt DOM读写XML文件

Qt DOM读写XML文件

2020-04-18