Qt中xml文件的使用

 

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

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

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

 

[cpp] view plain copy  在CODE上查看代码片派生到我的代码片

  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.   
  12. // print out the element names of all elements that are direct children  
  13. // of the outermost element.  
  14. QDomElement docElem = doc.documentElement();  
  15.   
  16. QDomNode n = docElem.firstChild();  
  17. while (!n.isNull())  
  18. {  
  19.     QDomElement e = n.toElement(); // try to convert the node to an element.  
  20.     if (!e.isNull())      
  21.     {  
  22.         cout << qPrintable(e.tagName()) << endl; // the node really is an element.  
  23.     }  
  24.     n = n.nextSibling();  
  25.   
  26. }  

 

 

 

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

 

[cpp] view plain copy  在CODE上查看代码片派生到我的代码片

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

 

 

 

[cpp] view plain copy  在CODE上查看代码片派生到我的代码片

  1. #include "dialog.h"  
  2. #include "ui_dialog.h"  
  3. #include <QFileDialog>  
  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.   
  31. void Dialog::openFile()  
  32. {  
  33.     QString fileName = QFileDialog::getOpenFileName(this, tr("Open XML File"), "c:/", tr("XML Files (*.xml)"));  
  34.     if(!fileName.isEmpty())  
  35.     {  
  36.         openXML( fileName );  
  37.     }  
  38. }  
  39.   
  40. Dialog::~Dialog()  
  41. {  
  42.     delete ui;  
  43. }  
  44.   
  45. void Dialog::listDom(QDomElement& docElem, QTreeWidgetItem * pItem)  
  46. {  
  47.     QDomNode node = docElem.firstChild();  
  48.     if(node.toElement().isNull())  
  49.     {  
  50.         pItem->setText (1, docElem.text());  
  51.     }  
  52.     while(!node.isNull())  
  53.     {  
  54.         QDomElement element = node.toElement(); // try to convert the node to an element.  
  55.         if( !element.isNull() )  
  56.         {  
  57.             QTreeWidgetItem *item;  
  58.             if( pItem )  
  59.             {  
  60.                 item = new QTreeWidgetItem(pItem);  
  61.             }  
  62.             else  
  63.             {  
  64.                 item = new QTreeWidgetItem(ui->treeWidget);  
  65.             }  
  66.             item->setText(0, element.tagName());  
  67.             listDom(element, item);  
  68.             if( pItem )  
  69.             {  
  70.                 pItem->addChild(item);  
  71.             }  
  72.             else  
  73.             {  
  74.                 ui->treeWidget->addTopLevelItem(item);  
  75.             }  
  76.         }  
  77.   
  78.         node = node.nextSibling();  
  79.     }  
  80.     return;  
  81. }  

 

下面是个测试 xml 文件:

 

[html] view plain copy  在CODE上查看代码片派生到我的代码片

  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>  

##### 欢迎关注问我团队公众号:

: 
技术在于交流、沟通,转载请注明出处并保持作品的完整性。 
作者:liyuanbhu 原文:http://blog.csdn.net/liyuanbhu/article/details/44062479

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值