使用Qt DOM更新XML文件

前端之家收集整理的这篇文章主要介绍了使用Qt DOM更新XML文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个应用程序,它构建一个基于树Qt组件的左菜单.为了加载它我需要解析 XML文件. XML文件如下所示:
  1. <comandos>
  2. <categoria>
  3. <nome>Cupom fiscal</nome>
  4. <comando>
  5. <nome>01 - Abrir Cupom Fiscal</nome>
  6. <env>3</env>
  7. <rec>4</rec>
  8. <desc>CNPJ / CPF : </desc>
  9. <desc>Nome : </desc>
  10. <desc>Endereco: </desc>
  11. </comando>
  12. </categoria>
  13. </comandos>

我实际上可以使用QtDOM读取这个XML.

  1. QDomDocument doc( "ComandosML" );
  2.  
  3. QFile file( "comandos.xml" );
  4.  
  5. int r = 0;
  6.  
  7. datafields.clear();
  8. receFields.clear();
  9. categories.clear();
  10.  
  11. if( !file.open( QIODevice::ReadOnly ) )
  12. return -1;
  13.  
  14. if( !doc.setContent( &file ) )
  15. {
  16. file.close();
  17. return -2;
  18. }
  19.  
  20. // Ok we are ready to parse DOM
  21. QDomElement root = doc.documentElement();
  22. if( root.tagName() != "comandos" )
  23. return -3;
  24.  
  25. QDomNode n = root.firstChild();
  26. while( !n.isNull() )
  27. {
  28. QDomElement e = n.toElement();
  29. if( !e.isNull() )
  30. {
  31. if( e.tagName() == "categoria" )
  32. {
  33. QDomNode cat = n.firstChild();
  34. while( !cat.isNull() )
  35. {
  36. QDomElement CatName = cat.toElement();
  37.  
  38. if ( CatName.tagName() == "nome")
  39. {
  40. QString s = CatName.text();
  41.  
  42. if ( s != "")
  43. {
  44. categories.push_back(s);
  45. item = new QStandardItem( (s) );
  46. item->setEditable(false);
  47. }
  48. }
  49.  
  50. if ( CatName.tagName() == "comando")
  51. {
  52.  
  53. QDomNode params = cat.firstChild();
  54. QString qdCmd;
  55. int env = 0;
  56. int rec = 0;
  57. Categories Desc;
  58.  
  59. while ( !params.isNull())
  60. {
  61. QDomElement ParamName = params.toElement();
  62.  
  63. if ( ParamName.tagName() == "nome")
  64. {
  65. qdCmd = ParamName.text();
  66. child = new QStandardItem( (qdCmd) );
  67. child->setEditable( false );
  68. child->setDragEnabled(false);
  69. item->appendRow( child );
  70. }
  71. else
  72. if ( ParamName.tagName() == "env")
  73. {
  74. env = ParamName.text().toInt();
  75. }
  76. else
  77. if ( ParamName.tagName() == "rec")
  78. {
  79. rec = ParamName.text().toInt();
  80. }
  81. else
  82. if ( ParamName.tagName() == "desc")
  83. {
  84. Desc.push_back(ParamName.text());
  85. }
  86.  
  87. params = params.nextSibling();
  88. }
  89.  
  90. datafields.insert(pair<QString,int>( qdCmd,env ));
  91. receFields.insert(pair<QString,rec ));
  92. descriptions.insert(pair<QString,Categories>( qdCmd,Desc) );
  93. }
  94. cat= cat.nextSibling();
  95. }
  96. model->setItem(r++,item);
  97. }
  98. }
  99. n = n.nextSibling();
  100. }
  101.  
  102. file.close();
  103.  
  104. return 0;

在解析之间我已经组装了菜单.毕竟,当用户编辑xml文件并在应用程序重新加载时,我已经准备好更新XML,我只需擦除树并再次重新创建它.你可以看到我也将一些数据传递给一些结构,它们基本上是std :: vector和std :: map.上面的代码是用Qt文档中的示例编写的,顺便说一句,这些代码相当不错.

碰巧我写了一个简单的对话框,让用户避免编辑XML.好吧,对我来说,即使从用户的角度来看,编辑XML也可能更容易和简单,但可能的用户更愿意在对话框上编辑内容.这一切都好.我可以抓取数据传递给应用程序.没问题.

但我需要更新XML.基本上,编辑将包括通过添加新节点或在子节点下插入子节点来更新节点.如何更新节点?有没有具体的方法来实现这一目标?我对XML的经验很简短,我经常编写,更新,解析txt和二进制文件.

我想做的事情如下:

  1. if( root.tagName() != "comandos" )
  2. return -3;
  3.  
  4. QDomNode n = root.firstChild();
  5. while( !n.isNull() )
  6. {
  7. QDomElement e = n.toElement();
  8. if( !e.isNull() )
  9. {
  10. if( e.tagName() == "categoria" )
  11. {
  12. QDomNode cat = n.firstChild();
  13. while( !cat.isNull() )
  14. {
  15.  
  16. QDomElement CatName = cat.toElement();
  17.  
  18. if ( CatName.tagName() == "nome")
  19. {
  20. QString s = CatName.text();
  21.  
  22. if ( s != qsCategory )
  23. {
  24. // we have not found the category
  25. // add it here
  26.  
  27. }
  28. else
  29. {
  30. // the category exists simply update
  31. }
  32.  
  33.  
  34.  
  35. }
  36.  
  37. cat= cat.nextSibling();
  38. }
  39. }
  40. }
  41. n = n.nextSibling();
  42. }

似乎使用Qt Dom非常适合解析和创建XML文件,但它缺少更新工具.任何帮助都会非常感激,甚至是一个例子.

这里的另一个帖子看起来很有用

Edit Value of a QDomElement?

我查看了互联网上有关更新XML文件的示例.看来,如果我抓住当前节点,我可以添加一个孩子,到目前为止我还没弄清楚如何这样做.

谢谢你的帮助,显然很抱歉我的无知.

解决方法

  1. QDomElement newCategoriaTag = doc.createElement(QString("categoria"));
  2. QDomElement newNoMetag = doc.createElement(QString("nome"));
  3. QDomText newNomeText = doc.createTextNode(QString("Cupom fiscal 2"));
  4. newNoMetag.appendChild(newNomeText);
  5. newCategoriaTag.appendChild(newNoMetag);
  6. root.appendChild(newCategoriaTag);

这将导致:

  1. <comandos>
  2. <categoria>
  3. <nome>Cupom fiscal</nome>
  4. <comando>
  5. <nome>01 - Abrir Cupom Fiscal</nome>
  6. <env>3</env>
  7. <rec>4</rec>
  8. <desc>CNPJ / CPF : </desc>
  9. <desc>Nome : </desc>
  10. <desc>Endereco: </desc>
  11. </comando>
  12. </categoria>
  13. <categoria>
  14. <nome>Cupom fiscal 2</nome>
  15. </categoria>
  16. </comandos>

猜你在找的HTML相关文章