|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
头文件:xmlstreamreader.h
#ifndef XMLSTREAMREADER_H
#define XMLSTREAMREADER_H
#include <QXmlStreamReader>
class QTreeWidget;
class QTreeWidgetItem;
class XmlStreamReader
{
public:
XmlStreamReader(QTreeWidget *tree);
bool readFile(const QString &fileName);
private:
void readBookindexElement();
void readEntryElement(QTreeWidgetItem *parent);
void readPageElement(QTreeWidgetItem *parent);
void skipUnknownElement();
QTreeWidget *treeWidget;
QXmlStreamReader reader;
};
#endif
CPP文件:xmlstreamreader.cpp
#include "xmlstreamreader.h"
#include <QtGui>
#include <QtXml>
#include <iostream>
XmlStreamReader::XmlStreamReader(QTreeWidget *tree)
{
treeWidget = tree;
}
bool XmlStreamReader::readFile(const QString &fileName)
{
QFile file(fileName);
if(!file.open(QFile::ReadOnly | QFile::Text))
{
std::cerr << "Error: Cannot read file " << qPrintable(fileName)
<< ": " << qPrintable(file.errorString())
<< std::endl;
return false;
}
reader.setDevice(&file);
reader.readNext();
while(!reader.atEnd())
{
if(reader.isStartElement())
{
if(reader.name() == "bookindex")
{
readBookindexElement();
}
else
{
reader.raiseError(QObject::tr("Not a bookindex file"));
}
}
else
{
reader.readNext();
}
}
file.close();
if(reader.hasError())
{
std::cerr << "Error: Failed to parse file "
<< qPrintable(fileName) << ": "
<< qPrintable(reader.errorString()) << std::endl;
return false;
}
else if(file.error() != QFile::NoError)
{
std::cerr << "Error: Cannot read file " << qPrintable(fileName)
<< ": " << qPrintable(file.errorString())
<< std::endl;
return false;
}
return true;
}
void XmlStreamReader::readBookindexElement()
{
reader.readNext();
while(!reader.atEnd())
{
if(reader.isEndElement())
{
reader.readNext();
break;
}
if(reader.isStartElement())
{
if(reader.name() == "entry")
{
readEntryElement(treeWidget->invisibleRootItem());
}
else
{
skipUnknownElement();
}
}
else
{
reader.readNext();
}
}
}
void XmlStreamReader::readEntryElement(QTreeWidgetItem *parent)
{
QTreeWidgetItem *item = new QTreeWidgetItem(parent);
item->setText(0, reader.attributes().value("term").toString());
reader.readNext();
while(!reader.atEnd())
{
if(reader.isEndElement())
{
reader.readNext();
break;
}
if(reader.isStartElement())
{
if(reader.name() == "entry")
{
readEntryElement(item);
}
else if(reader.name() == "page")
{
readPageElement(item);
}
else
{
skipUnknownElement();
}
}
else
{
reader.readNext();
}
}
}
void XmlStreamReader::readPageElement(QTreeWidgetItem *parent)
{
QString page = reader.readElementText();
if(reader.isEndElement())
reader.readNext();
QString allPages = parent->text(1);
if(!allPages.isEmpty())
allPages += ", ";
allPages += page;
parent->setText(1, allPages);
}
void XmlStreamReader::skipUnknownElement()
{
reader.readNext();
while(!reader.atEnd())
{
if(reader.isEndElement())
{
reader.readNext();
break;
}
if(reader.isStartElement())
{
skipUnknownElement();
}
else
{
reader.readNext();
}
}
}
MAIN文件:main.cpp
#include <QtGui>
#include <iostream>
#include <QtCore>
#include "xmlstreamreader.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QStringList args = QApplication::arguments();
if (args.count() > 2) {
std::cerr << "Usage: xmlstreamreader file1.xml..."
<< std::endl;
return 1;
}
QStringList labels;
labels << QObject::tr("Terms") << QObject::tr("Pages");
QTreeWidget treeWidget;
treeWidget.setHeaderLabels(labels);
treeWidget.header()->setResizeMode(QHeaderView::Stretch);
treeWidget.setWindowTitle(QObject::tr("XML Stream Reader"));
treeWidget.show();
XmlStreamReader reader(&treeWidget);
for (int i = 1; i < args.count(); ++i)
reader.readFile(args[i]);
return app.exec();
}
运行结果无内容:
MAIN文件:main.cpp
#include <QtGui>
#include <iostream>
#include <QtCore>
#include "xmlstreamreader.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QStringList labels;
labels << QObject::tr("Terms") << QObject::tr("Pages");
QTreeWidget treeWidget;
treeWidget.setHeaderLabels(labels);
treeWidget.header()->setResizeMode(QHeaderView::Stretch);
treeWidget.setWindowTitle(QObject::tr("XML Stream Reader"));
treeWidget.show();
XmlStreamReader reader(&treeWidget);
QString file("in1.xml");
reader.readFile(file);
return app.exec();
}
运行结果正常:
|
|