加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Qt操作xml文件(增删改功能)

发布时间:2020-12-15 23:30:35 所属栏目:百科 来源:网络整理
导读:这个例子是在根据网上博客《Qt数据库(XML)》改写的一个操作XML的实现。 借鉴了很多里面的代码,大家可以结合上面的博客对照,相信你肯定会对XML的操作熟练起来。 我建立的是Qwidget项目,没有添加ui文件,输出内容都放在应用程序输出中(qDebug)。 XMLtes

这个例子是在根据网上博客《Qt数据库(XML)》改写的一个操作XML的实现。

借鉴了很多里面的代码,大家可以结合上面的博客对照,相信你肯定会对XML的操作熟练起来。

我建立的是Qwidget项目,没有添加ui文件,输出内容都放在应用程序输出中(qDebug)。

XMLtest.pro文件代码:

[cpp] view plain copy
  1. #-------------------------------------------------
  2. #
  3. #ProjectcreatedbyQtCreator2012-08-15T15:56:54
  4. QT+=coreguixml
  5. TARGET=XMLtest
  6. TEMPLATE=app
  7. SOURCES+=main.cpp
  8. widget.cpp
  9. HEADERS+=widget.h


widget.h文件代码:

copy
    #ifndefWIDGET_H
  1. #defineWIDGET_H
  2. #include<QtGui/QWidget>
  3. #include<QtCore>
  4. classWidget:publicQWidget
  5. {
  6. Q_OBJECT
  7. public:
  8. Widget(QWidget*parent=0);
  9. ~Widget();
  10. voidread_xml(QStringfilename);
  11. voidcreate_xml(QStringfilename);
  12. voidadd_xmlnode(QStringfilename,QStringrmt_name,QStringipa,QStringipb);
  13. voiddo_xml(constQStringopt,QStringfilename);
  14. private:
  15. };
  16. #endif//WIDGET_H


widget.cpp文件代码:

copy
    #include"widget.h"
  1. #include"qfile.h"
  2. #include"qdebug.h"
  3. #include<QDomDocument>
  4. #include"qtextcodec.h"
  5. Widget::Widget(QWidget*parent)
  6. :QWidget(parent)
  7. QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));
  8. QFile*file;
  9. QStringfilename="config.xml";
  10. if(file->exists("config.xml"))
  11. read_xml(filename);
  12. }
  13. else
  14. create_xml(filename);
  15. add_xmlnode(filename,"remote1","127.0.0.1","192.168.1.199");
  16. do_xml("update",filename);
  17. Widget::~Widget()
  18. {
  19. }
  20. voidWidget::do_xml( QFilefile(filename);
  21. if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
  22. qDebug()<<"openfordoerro";
  23. file.close();
  24. QDomDocumentdoc;
  25. if(!doc.setContent(&file))
  26. qDebug()<<"setcontentfordoerror";
  27. file.close();
  28. QDomNodeListlists=doc.elementsByTagName("remote");
  29. QDomElementele=lists.at(1).toElement();
  30. if(ele.attribute(tr("id"))=="3")
  31. if("delete"==opt||"update"==opt)
  32. QDomElementroot=doc.documentElement();
  33. if("delete"==opt)
  34. root.removeChild(lists.at(1));
  35. qDebug()<<"removeok!";
  36. QDomNodeListchild=lists.at(1).childNodes();
  37. child.at(0).toElement().firstChild().setNodeValue("namechanged");
  38. child.at(1).toElement().firstChild().setNodeValue("ipachanged");
  39. child.at(2).toElement().firstChild().setNodeValue("ipbchanged");
  40. qDebug()<<"modifyok!";
  41. if(!file.open(QIODevice::WriteOnly|QIODevice::Text))
  42. qDebug()<<"openforremoveerror!";
  43. QTextStreamout(&file);
  44. doc.save(out,4);
  45. voidWidget::add_xmlnode(QStringfilename,QStringipb)
  46. if(!file.open(QIODevice::ReadOnly|QFile::Text)){
  47. qDebug()<<"openforadderror...";
  48. QDomDocumentdoc;
  49. QStringerrorStr;
  50. interrorLine;
  51. interrorColumn;
  52. if(!doc.setContent(&file,false,&errorStr,&errorLine,&errorColumn)){
  53. qDebug()<<"addsetcontenterror...";
  54. //QDomNodenode=doc.firstChild();
  55. QDomElementroot=doc.documentElement();
  56. if(root.isNull())
  57. root=doc.createElement("ipconfig");
  58. QDomElementelement_root=doc.createElement(tr("remote"));
  59. QDomAttrattr_id=doc.createAttribute(tr("id"));
  60. QDomElementelement_rmt=doc.createElement(tr("rmt_name"));
  61. QDomElementelement_ipa=doc.createElement(tr("ipa"));
  62. QDomElementelement_ipb=doc.createElement(tr("ipb"));
  63. QStringstr_id;
  64. if(root.lastChild().isNull())
  65. str_id="1";
  66. attr_id.setValue(str_id);
  67. else
  68. str_id=root.lastChild().toElement().attribute(tr("id"));
  69. intcount=str_id.toInt()+1;
  70. attr_id.setValue(QString::number(count));
  71. QDomTexttext;
  72. text=doc.createTextNode(rmt_name);
  73. element_rmt.appendChild(text);
  74. text=doc.createTextNode(ipa);
  75. element_ipa.appendChild(text);
  76. text=doc.createTextNode(ipb);
  77. element_ipb.appendChild(text);
  78. text.clear();
  79. element_root.appendChild(element_rmt);
  80. element_root.appendChild(element_ipa);
  81. element_root.appendChild(element_ipb);
  82. element_root.setAttributeNode(attr_id);
  83. root.appendChild(element_root);
  84. if(!file.open(QIODevice::WriteOnly|QIODevice::Append))
  85. qDebug()<<"openforadderror!";
  86. QTextStreamout(&file);
  87. doc.save(out,4);
  88. voidWidget::read_xml(QStringfilename)
  89. qDebug()<<"openforreaderror...";
  90. QStringerrorStr;
  91. interrorLine;
  92. interrorColumn;
  93. qDebug()<<"setcontenterror...";
  94. if(root.tagName()!="ipconfig"){
  95. qDebug()<<"root.tagname!=ipconfig...";
  96. QDomNodenode=root.firstChild();
  97. while(!node.isNull())
  98. if(node.isElement())
  99. QDomElementelement=node.toElement();
  100. qDebug()<<qPrintable(element.tagName())<<qPrintable(element.attribute("id"));
  101. QDomNodeListlist=element.childNodes();
  102. for(inti=0;i<list.count();i++)
  103. QDomNodenodechild=list.at(i);
  104. if(nodechild.isElement())
  105. qDebug()<<""<<qPrintable(nodechild.toElement().tagName())<<qPrintable(nodechild.toElement().text());
  106. node=node.nextSibling();
  107. voidWidget::create_xml(QStringfilename)
  108. QFilefile(filename);
  109. file.open(QIODevice::ReadWrite);
  110. QDomProcessingInstructioninstruction;
  111. instruction=doc.createProcessingInstruction("xml","version="1.0"encoding="GB2312"");
  112. doc.appendChild(instruction);
  113. QDomElementroot=doc.createElement("ipconfig");
  114. doc.appendChild(root);
  115. QDomTexttext=doc.createTextNode("");
  116. root.appendChild(text);
  117. }


main.cpp文件代码:

copy
    #include<QtGui/QApplication>
  1. #include"widget.h"
  2. intmain(intargc,char*argv[])
  3. QApplicationa(argc,argv);
  4. Widgetw;
  5. w.show();
  6. returna.exec();
  7. XML文件结构:

    [html] copy
      <?xmlversion='1.0'encoding='GB2312'?>
    1. <ipconfig>
    2. remoteid="1">
    3. rmt_name>remote1</ipa>127.0.0.1ipb>192.168.1.199remoteremoteid="3">namechanged>ipachanged>ipbchangedremoteid="4"remoteid="5"remoteid="6"remoteid="7"remoteid="8">


    应用程序输出:

    remote 1

    rmt_name remote1

    ipa 127.0.0.1

    ipb 192.168.1.199

    remote 3

    remote 4

    remote 5

    remote 6

    remote 7

    modify ok !

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读