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
#include
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
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文件:
Ice Cream Sundae
0.5
vanilla ice cream
3
chocolate syrup or chocolate fudge
1
nuts
1
cherry
1
bowl
1
spoons
1
ice cream scoop
Using ice cream scoop, place vanilla ice cream into bowl.
Drizzle chocolate syrup or chocolate fudge over the ice cream.
Sprinkle nuts over the mound of chocolate and ice cream.
Place cherry on top of mound with stem pointing upward.
Serve.
Replace nuts with raisins.
Use chocolate ice cream instead of vanilla ice cream.
5 minutes
下面是软件界面: