当前位置:   article > 正文

Qt5 对xml文件常用的操作(读写,增删改查)_qt 更新xml

qt 更新xml

微信搜索关注公众号 “郑州行疆户外” 了解软件工程师的业余户外生活

项目配置

pro文件里面添加QT+=xml

include <QtXml>,也可以include <QDomDocument>

项目文件:

.pro 文件

  1. QT += core xml
  2. QT -= gui
  3. TARGET = xmltest
  4. CONFIG += console
  5. CONFIG -= app_bundle
  6. TEMPLATE = app
  7. SOURCES += main.cpp

主程序:

main.cpp

  1. #include <QCoreApplication>
  2. #include <QtXml> //也可以include <QDomDocument>
  3. //写xml
  4. void WriteXml()
  5. {
  6. //打开或创建文件
  7. QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
  8. if(!file.open(QFile::WriteOnly|QFile::Truncate)) //可以用QIODevice,Truncate表示清空原来的内容
  9. return;
  10. QDomDocument doc;
  11. //写入xml头部
  12. QDomProcessingInstruction instruction; //添加处理命令
  13. instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
  14. doc.appendChild(instruction);
  15. //添加根节点
  16. QDomElement root=doc.createElement("library");
  17. doc.appendChild(root);
  18. //添加第一个子节点及其子元素
  19. QDomElement book=doc.createElement("book");
  20. book.setAttribute("id",1); //方式一:创建属性 其中键值对的值可以是各种类型
  21. QDomAttr time=doc.createAttribute("time"); //方式二:创建属性 值必须是字符串
  22. time.setValue("2013/6/13");
  23. book.setAttributeNode(time);
  24. QDomElement title=doc.createElement("title"); //创建子元素
  25. QDomText text; //设置括号标签中间的值
  26. text=doc.createTextNode("C++ primer");
  27. book.appendChild(title);
  28. title.appendChild(text);
  29. QDomElement author=doc.createElement("author"); //创建子元素
  30. text=doc.createTextNode("Stanley Lippman");
  31. author.appendChild(text);
  32. book.appendChild(author);
  33. root.appendChild(book);
  34. //添加第二个子节点及其子元素,部分变量只需重新赋值
  35. book=doc.createElement("book");
  36. book.setAttribute("id",2);
  37. time=doc.createAttribute("time");
  38. time.setValue("2007/5/25");
  39. book.setAttributeNode(time);
  40. title=doc.createElement("title");
  41. text=doc.createTextNode("Thinking in Java");
  42. book.appendChild(title);
  43. title.appendChild(text);
  44. author=doc.createElement("author");
  45. text=doc.createTextNode("Bruce Eckel");
  46. author.appendChild(text);
  47. book.appendChild(author);
  48. root.appendChild(book);
  49. //输出到文件
  50. QTextStream out_stream(&file);
  51. doc.save(out_stream,4); //缩进4格
  52. file.close();
  53. }
  54. //读xml
  55. void ReadXml()
  56. {
  57. //打开或创建文件
  58. QFile file("test.xml"); //相对路径、绝对路径、资源路径都行
  59. if(!file.open(QFile::ReadOnly))
  60. return;
  61. QDomDocument doc;
  62. if(!doc.setContent(&file))
  63. {
  64. file.close();
  65. return;
  66. }
  67. file.close();
  68. QDomElement root=doc.documentElement(); //返回根节点
  69. qDebug()<<root.nodeName();
  70. QDomNode node=root.firstChild(); //获得第一个子节点
  71. while(!node.isNull()) //如果节点不空
  72. {
  73. if(node.isElement()) //如果节点是元素
  74. {
  75. QDomElement e=node.toElement(); //转换为元素,注意元素和节点是两个数据结构,其实差不多
  76. qDebug()<<e.tagName()<<" "<<e.attribute("id")<<" "<<e.attribute("time"); //打印键值对,tagName和nodeName是一个东西
  77. QDomNodeList list=e.childNodes();
  78. for(int i=0;i<list.count();i++) //遍历子元素,count和size都可以用,可用于标签数计数
  79. {
  80. QDomNode n=list.at(i);
  81. if(node.isElement())
  82. qDebug()<<n.nodeName()<<":"<<n.toElement().text();
  83. }
  84. }
  85. node=node.nextSibling(); //下一个兄弟节点,nextSiblingElement()是下一个兄弟元素,都差不多
  86. }
  87. }
  88. //增加xml内容
  89. void AddXml()
  90. {
  91. //打开文件
  92. QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
  93. if(!file.open(QFile::ReadOnly))
  94. return;
  95. //增加一个一级子节点以及元素
  96. QDomDocument doc;
  97. if(!doc.setContent(&file))
  98. {
  99. file.close();
  100. return;
  101. }
  102. file.close();
  103. QDomElement root=doc.documentElement();
  104. QDomElement book=doc.createElement("book");
  105. book.setAttribute("id",3);
  106. book.setAttribute("time","1813/1/27");
  107. QDomElement title=doc.createElement("title");
  108. QDomText text;
  109. text=doc.createTextNode("Pride and Prejudice");
  110. title.appendChild(text);
  111. book.appendChild(title);
  112. QDomElement author=doc.createElement("author");
  113. text=doc.createTextNode("Jane Austen");
  114. author.appendChild(text);
  115. book.appendChild(author);
  116. root.appendChild(book);
  117. if(!file.open(QFile::WriteOnly|QFile::Truncate)) //先读进来,再重写,如果不用truncate就是在后面追加内容,就无效了
  118. return;
  119. //输出到文件
  120. QTextStream out_stream(&file);
  121. doc.save(out_stream,4); //缩进4格
  122. file.close();
  123. }
  124. //删减xml内容
  125. void RemoveXml()
  126. {
  127. //打开文件
  128. QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
  129. if(!file.open(QFile::ReadOnly))
  130. return;
  131. //删除一个一级子节点及其元素,外层节点删除内层节点于此相同
  132. QDomDocument doc;
  133. if(!doc.setContent(&file))
  134. {
  135. file.close();
  136. return;
  137. }
  138. file.close(); //一定要记得关掉啊,不然无法完成操作
  139. QDomElement root=doc.documentElement();
  140. QDomNodeList list=doc.elementsByTagName("book"); //由标签名定位
  141. for(int i=0;i<list.count();i++)
  142. {
  143. QDomElement e=list.at(i).toElement();
  144. if(e.attribute("time")=="2007/5/25") //以属性名定位,类似于hash的方式,warning:这里仅仅删除一个节点,其实可以加个break
  145. root.removeChild(list.at(i));
  146. }
  147. if(!file.open(QFile::WriteOnly|QFile::Truncate))
  148. return;
  149. //输出到文件
  150. QTextStream out_stream(&file);
  151. doc.save(out_stream,4); //缩进4格
  152. file.close();
  153. }
  154. //更新xml内容
  155. void UpdateXml()
  156. {
  157. //打开文件
  158. QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
  159. if(!file.open(QFile::ReadOnly))
  160. return;
  161. //更新一个标签项,如果知道xml的结构,直接定位到那个标签上定点更新
  162. //或者用遍历的方法去匹配tagname或者attribut,value来更新
  163. QDomDocument doc;
  164. if(!doc.setContent(&file))
  165. {
  166. file.close();
  167. return;
  168. }
  169. file.close();
  170. QDomElement root=doc.documentElement();
  171. QDomNodeList list=root.elementsByTagName("book");
  172. QDomNode node=list.at(list.size()-1).firstChild(); //定位到第三个一级子节点的子元素
  173. QDomNode oldnode=node.firstChild(); //标签之间的内容作为节点的子节点出现,当前是Pride and Projudice
  174. node.firstChild().setNodeValue("Emma");
  175. QDomNode newnode=node.firstChild();
  176. node.replaceChild(newnode,oldnode);
  177. if(!file.open(QFile::WriteOnly|QFile::Truncate))
  178. return;
  179. //输出到文件
  180. QTextStream out_stream(&file);
  181. doc.save(out_stream,4); //缩进4格
  182. file.close();
  183. }
  184. int main(int argc, char *argv[])
  185. {
  186. qDebug()<<"write xml to file...";
  187. WriteXml();
  188. qDebug()<<"read xml to display...";
  189. ReadXml();
  190. qDebug()<<"add contents to xml...";
  191. AddXml();
  192. qDebug()<<"remove contents from xml...";
  193. RemoveXml();
  194. qDebug()<<"update contents to xml...";
  195. UpdateXml();
  196. return 0;
  197. }
  198.  

写xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <library>
  3. <book id="1" time="2013/6/13">
  4. <title>C++ primer</title>
  5. <author>Stanley Lippman</author>
  6. </book>
  7. <book id="2" time="2007/5/25">
  8. <title>Thinking in Java</title>
  9. <author>Bruce Eckel</author>
  10. </book>
  11. </library>

增加xml

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <library>
  3. <book time="2013/6/13" id="1">
  4. <title>C++ primer</title>
  5. <author>Stanley Lippman</author>
  6. </book>
  7. <book time="2007/5/25" id="2">
  8. <title>Thinking in Java</title>
  9. <author>Bruce Eckel</author>
  10. </book>
  11. <book time="1813/1/27" id="3">
  12. <title>Pride and Prejudice</title>
  13. <author>Jane Austen</author>
  14. </book>
  15. </library>

删除xml

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <library>
  3. <book time="2013/6/13" id="1">
  4. <title>C++ primer</title>
  5. <author>Stanley Lippman</author>
  6. </book>
  7. <book time="2007/5/25" id="2">
  8. <title>Thinking in Java</title>
  9. <author>Bruce Eckel</author>
  10. </book>
  11. <book time="1813/1/27" id="3">
  12. <title>Pride and Prejudice</title>
  13. <author>Jane Austen</author>
  14. </book>
  15. </library>
更新xml
  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <library>
  3. <book id="1" time="2013/6/13">
  4. <title>C++ primer</title>
  5. <author>Stanley Lippman</author>
  6. </book>
  7. <book id="3" time="1813/1/27">
  8. <title>Emma</title>
  9. <author>Jane Austen</author>
  10. </book>
  11. </library>

如果觉得写得不错,烦请微信搜索公众号 "郑州行疆户外" 了解程序员的户外业余喜好,鼓励一下。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/781913
推荐阅读
相关标签
  

闽ICP备14008679号