利用 Qt 读取 XML 文件的方法

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

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

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

QDomDocument doc("mydocument");
QFile file("mydocument.xml");
if (!file.open(QIODevice::ReadOnly))
return;
if (!doc.setContent(&file))
{
file.close();
return;
}
file.close();

// print out the element names of all elements that are direct children
// of the outermost element.
QDomElement docElem = doc.documentElement();

QDomNode n = docElem.firstChild();
while (!n.isNull())
{
QDomElement e = n.toElement(); // try to convert the node to an element.
if (!e.isNull())
{
cout << qPrintable(e.tagName()) << endl; // the node really is an element.
}
n = n.nextSibling();

}

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

#ifndef DIALOG_H
#define DIALOG_H
#include <QtXml>
#include <QDialog>

namespace Ui {
class Dialog;
}
class QTreeWidgetItem;
class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0);
void listDom(QDomElement& docElem, QTreeWidgetItem* pItem);
void openXML(QString fileName);
~Dialog();

private:
Ui::Dialog *ui;
private slots:
void openFile();
};

#endif // DIALOG_H

#include "dialog.h"
#include "ui_dialog.h"
#include <QFileDialog>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->pushButtonOpen, SIGNAL(clicked()), this, SLOT(openFile()));
ui->treeWidget->setColumnCount(2);
ui->treeWidget->setColumnWidth(0, 400);
setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
showMaximized();
}
void Dialog::openXML(QString fileName)
{
QFile file(fileName);
if(file.open(QIODevice::ReadOnly))
{
QDomDocument dom("WCM");
if (dom.setContent(&file))
{
ui->treeWidget->clear();
QDomElement docElem = dom.documentElement();
listDom(docElem, NULL);
}
}
file.close();
}

void Dialog::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open XML File"), "c:/", tr("XML Files (*.xml)"));
if(!fileName.isEmpty())
{
openXML( fileName );
}
}

Dialog::~Dialog()
{
delete ui;
}

void Dialog::listDom(QDomElement& docElem, QTreeWidgetItem * pItem)
{
QDomNode node = docElem.firstChild();
if(node.toElement().isNull())
{
pItem->setText (1, docElem.text());
}
while(!node.isNull())
{
QDomElement element = node.toElement(); // try to convert the node to an element.
if( !element.isNull() )
{
QTreeWidgetItem *item;
if( pItem )
{
item = new QTreeWidgetItem(pItem);
}
else
{
item = new QTreeWidgetItem(ui->treeWidget);
}
item->setText(0, element.tagName());
listDom(element, item);
if( pItem )
{
pItem->addChild(item);
}
else
{
ui->treeWidget->addTopLevelItem(item);
}
}

node = node.nextSibling();
}
return;
}

下面是个测试 xml 文件:

<?xml version="1.0" encoding="UTF⑻"?>
<recipe type="dessert">
<recipename cuisine="american" servings="1">Ice Cream Sundae</recipename>
<ingredlist>
<listitem>
<quantity units="cups">0.5</quantity>
<itemdescription>vanilla ice cream</itemdescription>
</listitem>
<listitem>
<quantity units="tablespoons">3</quantity>
<itemdescription>chocolate syrup or chocolate fudge</itemdescription>
</listitem>
<listitem>
<quantity units="tablespoons">1</quantity>
<itemdescription>nuts</itemdescription>
</listitem>
<listitem>
<quantity units="each">1</quantity>
<itemdescription>cherry</itemdescription>
</listitem>
</ingredlist>
<utensils>
<listitem>
<quantity units="each">1</quantity>
<utensilname>bowl</utensilname>
</listitem>
<listitem>
<quantity units="each">1</quantity>
<utensilname>spoons</utensilname>
</listitem>
<listitem>
<quantity units="each">1</quantity>
<utensilname>ice cream scoop</utensilname>
</listitem>
</utensils>
<directions>
<step>Using ice cream scoop, place vanilla ice cream into bowl.</step>
<step>Drizzle chocolate syrup or chocolate fudge over the ice cream.</step>
<step>Sprinkle nuts over the mound of chocolate and ice cream.</step>
<step>Place cherry on top of mound with stem pointing upward.</step>
<step>Serve.</step>
</directions>
<variations>
<option>Replace nuts with raisins.</option>
<option>Use chocolate ice cream instead of vanilla ice cream.</option>
</variations>
<preptime>5 minutes</preptime>
</recipe>


下面是软件界面:

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » 利用 Qt 读取 XML 文件的方法

80 评论

  1. purchase avodart generic flomax us brand ondansetron 4mg

  2. order ampicillin 250mg pill erythromycin sale order erythromycin 500mg

  3. purchase suhagra online cheap cheap estrace 2mg buy estrace 2mg generic

  4. buy dapsone 100 mg sale avlosulfon us order perindopril pill

  5. chloroquine over the counter oral aralen order baricitinib 2mg without prescription

  6. norvasc 10mg without prescription female viagra cvs generic tadalafil 5mg

  7. cialis canada cialis uk viagra 100mg pill

  8. order tadalafil 20mg sale real cialis sites order clopidogrel 75mg online

  9. imitrex 50mg pill levaquin price purchase avodart pills

  10. purchase ranitidine generic oral zantac 150mg buy celecoxib 100mg without prescription

  11. where can i buy an essay edit letter buy ivermectin 3mg for humans

  12. order azithromycin 500mg online gabapentin sale cheap neurontin sale

  13. order tadalafil 10mg online cheap Cialis use online blackjack real money

  14. order indomethacin online buy cenforce sale purchase cenforce generic

  15. doxycycline 200mg without prescription doxycycline for sale methylprednisolone tablet

  16. order ropinirole 1mg generic calcitriol pills trandate 100mg brand

  17. cheap fenofibrate tricor tablet sildenafil citrate 50 mg

  18. buy cialis 5mg generic Free cialis sildenafil 50mg tablets

  19. buy albuterol sale albuterol order augmentin 375mg generic

  20. order zetia 10mg pill sumycin oral methotrexate 2.5mg cost

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡