40.DOM读取XML
main.cpp
#include <QtGui> #include <iostream> #include "domparser.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); QDir::setCurrent(app.applicationDirPath());//设置路径为当前目录 QStringList labels; labels << QObject::tr("Terms") << QObject::tr("Pages"); QTreeWidget treeWidget; treeWidget.setHeaderLabels(labels); treeWidget.header()->setResizeMode(QHeaderView::Stretch); treeWidget.setWindowTitle(QObject::tr("DOM Parser")); treeWidget.show(); DomParser parser(&treeWidget); parser.readFile("in1.xml"); return app.exec(); }
DomPaser.h
#ifndef DOMPARSER_H #define DOMPARSER_H class QDomElement; class QString; class QTreeWidget; class QTreeWidgetItem; class DomParser { public: DomParser(QTreeWidget *tree); //打开传递来的文件 bool readFile(const QString &fileName); private: void parseBookindexElement(const QDomElement &element); void parseEntryElement(const QDomElement &element, QTreeWidgetItem *parent); void parsePageElement(const QDomElement &element, QTreeWidgetItem *parent); QTreeWidget *treeWidget; }; #endif
domparser.cpp
#include <QtGui> #include <QtXml> #include <iostream> #include "domparser.h" DomParser::DomParser(QTreeWidget *tree) { treeWidget = tree; } //读文件 bool DomParser::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; } //错误信息 QString errorStr; //错误行 int errorLine; //错误项 int errorColumn; QDomDocument doc; //setContent解析XML文档,如果文档没打开, //setContent将自动打开,false参数将禁用命名空间的处理. if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn)) { std::cerr << "Error: Parse error at line " << errorLine << ", " << "column " << errorColumn << ": " << qPrintable(errorStr) << std::endl; return false; } //如果解析成功,就对QDomDocument调用documentElement获取它唯一的 //QDomElement子对象 QDomElement root = doc.documentElement(); //判断是否有<bookindex>元素,如果有就调用QDomElement来解析 if (root.tagName() != "bookindex") { std::cerr << "Error: Not a bookindex file" << std::endl; return false; } parseBookindexElement(root); return true; } //遍历所有子节点 void DomParser::parseBookindexElement(const QDomElement &element) { //发现下一个子对象 QDomNode child = element.firstChild(); //如果不为空 while (!child.isNull()) { if (child.toElement().tagName() == "entry") { parseEntryElement(child.toElement(), treeWidget->invisibleRootItem()); } child = child.nextSibling(); } } //entry标签处理 void DomParser::parseEntryElement(const QDomElement &element, QTreeWidgetItem *parent) { QTreeWidgetItem *item = new QTreeWidgetItem(parent); item->setText(0, element.attribute("term")); QDomNode child = element.firstChild(); while (!child.isNull()) { //如果是entry调用entry处理 if (child.toElement().tagName() == "entry") { parseEntryElement(child.toElement(), item); } //如果是page调用page处理 else if (child.toElement().tagName() == "page") { parsePageElement(child.toElement(), item); } //继续处理下一个 child = child.nextSibling(); } } //处理page项 void DomParser::parsePageElement(const QDomElement &element, QTreeWidgetItem *parent) { QString page = element.text(); QString allPages = parent->text(1); if (!allPages.isEmpty()) allPages += ", "; allPages += page; parent->setText(1, allPages); }