用 Qt 读取 XML 文件的方法

原文:https://blog.csdn.net/liyuanbhu/article/details/44062479

XML 是可扩展标记语言(Extensible Markup Language)的缩写。XML 文件由内容和标记组成,通过以标记包围内容的方式将大部分内容包含在元素中。

Qt 中提供了多种读取XML文件的方法,这里简单的记录一下用 QDomDocument 读取的步骤。为什么使用QDomDocument 呢,因为XML 本身就是一以树状结构组织数据的,而DOM 也是将数据组织为树状结构,最适合直观地展示XML数据。

下面的代码是Qt 帮助文件中自带的例子代码:


     
     
     
     
  1. QDomDocument doc("mydocument");
  2. QFile file("mydocument.xml");
  3. if (!file.open(QIODevice::ReadOnly))
  4. return;
  5. if (!doc.setContent(&file))
  6. {
  7. file.close();
  8. return;
  9. }
  10. file.close();
  11. // print out the element names of all elements that are direct children
  12. // of the outermost element.
  13. QDomElement docElem = doc.documentElement();
  14. QDomNode n = docElem.firstChild();
  15. while (!n.isNull())
  16. {
  17. QDomElement e = n.toElement(); // try to convert the node to an element.
  18. if (!e.isNull())
  19. {
  20. cout << qPrintable(e.tagName()) << endl; // the node really is an element.
  21. }
  22. n = n.nextSibling();
  23. }

如果xml有多层,那么可以递归的去读取。我写了小程序将xml 的的数据读入到一个树型列表控件中。下面是核心的代码:


     
     
     
     
  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include
  4. #include
  5. namespace Ui {
  6. class Dialog;
  7. }
  8. class QTreeWidgetItem;
  9. class Dialog : public QDialog
  10. {
  11. Q_OBJECT
  12. public:
  13. explicit Dialog(QWidget *parent = 0);
  14. void listDom(QDomElement& docElem, QTreeWidgetItem* pItem);
  15. void openXML(QString fileName);
  16. ~Dialog();
  17. private:
  18. Ui::Dialog *ui;
  19. private slots:
  20. void openFile();
  21. };
  22. #endif // DIALOG_H


     
     
     
     
  1. #include "dialog.h"
  2. #include "ui_dialog.h"
  3. #include
  4. Dialog::Dialog(QWidget *parent) :
  5. QDialog(parent),
  6. ui( new Ui::Dialog)
  7. {
  8. ui->setupUi( this);
  9. connect(ui->pushButtonOpen, SIGNAL(clicked()), this, SLOT(openFile()));
  10. ui->treeWidget->setColumnCount( 2);
  11. ui->treeWidget->setColumnWidth( 0, 400);
  12. setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
  13. showMaximized();
  14. }
  15. void Dialog::openXML(QString fileName)
  16. {
  17. QFile file(fileName);
  18. if(file.open(QIODevice::ReadOnly))
  19. {
  20. QDomDocument dom("WCM");
  21. if (dom.setContent(&file))
  22. {
  23. ui->treeWidget->clear();
  24. QDomElement docElem = dom.documentElement();
  25. listDom(docElem, NULL);
  26. }
  27. }
  28. file.close();
  29. }
  30. void Dialog::openFile()
  31. {
  32. QString fileName = QFileDialog::getOpenFileName( this, tr( "Open XML File"), "c:/", tr( "XML Files (*.xml)"));
  33. if(!fileName.isEmpty())
  34. {
  35. openXML( fileName );
  36. }
  37. }
  38. Dialog::~Dialog()
  39. {
  40. delete ui;
  41. }
  42. void Dialog::listDom(QDomElement& docElem, QTreeWidgetItem * pItem)
  43. {
  44. QDomNode node = docElem.firstChild();
  45. if(node.toElement().isNull())
  46. {
  47. pItem->setText ( 1, docElem.text());
  48. }
  49. while(!node.isNull())
  50. {
  51. QDomElement element = node.toElement(); // try to convert the node to an element.
  52. if( !element.isNull() )
  53. {
  54. QTreeWidgetItem *item;
  55. if( pItem )
  56. {
  57. item = new QTreeWidgetItem(pItem);
  58. }
  59. else
  60. {
  61. item = new QTreeWidgetItem(ui->treeWidget);
  62. }
  63. item->setText( 0, element.tagName());
  64. listDom(element, item);
  65. if( pItem )
  66. {
  67. pItem->addChild(item);
  68. }
  69. else
  70. {
  71. ui->treeWidget->addTopLevelItem(item);
  72. }
  73. }
  74. node = node.nextSibling();
  75. }
  76. return;
  77. }

下面是个测试 xml 文件:


     
     
     
     
  1. xml version="1.0" encoding="UTF-8"?>
  2. <recipe type="dessert">
  3. <recipename cuisine="american" servings="1">Ice Cream Sundae recipename>
  4. <ingredlist>
  5. <listitem>
  6. <quantity units="cups">0.5 quantity>
  7. <itemdescription>vanilla ice cream itemdescription>
  8. listitem>
  9. <listitem>
  10. <quantity units="tablespoons">3 quantity>
  11. <itemdescription>chocolate syrup or chocolate fudge itemdescription>
  12. listitem>
  13. <listitem>
  14. <quantity units="tablespoons">1 quantity>
  15. <itemdescription>nuts itemdescription>
  16. listitem>
  17. <listitem>
  18. <quantity units="each">1 quantity>
  19. <itemdescription>cherry itemdescription>
  20. listitem>
  21. ingredlist>
  22. <utensils>
  23. <listitem>
  24. <quantity units="each">1 quantity>
  25. <utensilname>bowl utensilname>
  26. listitem>
  27. <listitem>
  28. <quantity units="each">1 quantity>
  29. <utensilname>spoons utensilname>
  30. listitem>
  31. <listitem>
  32. <quantity units="each">1 quantity>
  33. <utensilname>ice cream scoop utensilname>
  34. listitem>
  35. utensils>
  36. <directions>
  37. <step>Using ice cream scoop, place vanilla ice cream into bowl. step>
  38. <step>Drizzle chocolate syrup or chocolate fudge over the ice cream. step>
  39. <step>Sprinkle nuts over the mound of chocolate and ice cream. step>
  40. <step>Place cherry on top of mound with stem pointing upward. step>
  41. <step>Serve. step>
  42. directions>
  43. <variations>
  44. <option>Replace nuts with raisins. option>
  45. <option>Use chocolate ice cream instead of vanilla ice cream. option>
  46. variations>
  47. <preptime>5 minutes preptime>
  48. recipe>


下面是软件界面:
用 Qt 读取 XML 文件的方法_第1张图片

你可能感兴趣的:(Qt,XML)