开始加入类来处理各种元素

Signed-off-by: facat@lab.com <facat@lab.com>
This commit is contained in:
facat@lab.com 2014-11-22 11:32:12 +08:00
parent 3a555bd2c1
commit f502f0dc62
8 changed files with 182 additions and 3 deletions

View File

@ -0,0 +1,28 @@
#include "BasicElementInfo.h"
//#include <iostream>
bool BasicElementInfo::parseBasicInfo(QXmlStreamReader &reader)
{
// std::cout<<"father"<<"\n";
QStringRef elementName;
QStringRef id;
elementName=reader.name();
qDebug()<<reader.name()<<"\n";
if(reader.attributes().hasAttribute("rdf:ID"))
{
qDebug()<<reader.attributes().value("rdf:ID")<<"\n";
id=reader.attributes().value("rdf:ID");
}
while(!reader.atEnd() && !reader.hasError())
{
reader.readNext();
if(reader.isEndElement() && reader.name()==elementName)
{
break;
}
}
this->name=elementName;
this->id=id;
return true;
}

View File

@ -0,0 +1,19 @@
#ifndef BASICELEMENTINFO_H
#define BASICELEMENTINFO_H
#include <QString>
#include <QStringRef>
#include <QXmlStreamReader>
#include <QDebug>
class BasicElementInfo{
protected:
bool parseBasicInfo(QXmlStreamReader &reader);
QStringRef name;
QStringRef id;
QString nextLinkString;
QString prevousLinkString;
};
#endif // BASICELEMENTINFO_H

View File

@ -0,0 +1,65 @@
#include "elementhashtable.h"
//#include <iostream>
ElementHashtable::ElementHashtable(QObject *parent) :
QObject(parent)
{
}
bool ElementHashtable::child(QXmlStreamReader &reader)
{
QStringRef elementName;
elementName=reader.name();
BasicElementInfo *pointer=NULL;
if(elementName.toString()=="SubControlArea")
{
SubControlArea sa;
pointer=&sa;
sa.parse(reader);
}
if(pointer)
{
this->eleHT[elementName.toString()]=*pointer;
}
return true;
}
bool ElementHashtable::parse(const QString& xmlPath)
{
QFile xmlFile(xmlPath);
int loop=0;
if(xmlFile.open(QFile::ReadOnly))
{
QXmlStreamReader reader(&xmlFile);
while(!reader.atEnd() && !reader.hasError())
{
reader.readNext();
if(reader.isStartElement())
{
if(reader.name()=="RDF")//不要读<rdf:RDF xmlns:rdf="http://www.w3.org/1999/ …… 这样一堆东西
{
continue;
}
this->child(reader);//处理根元素下的第一级子元素也就是Substation这一类。
}
loop++;
if(loop>20){
break;
}
}
if(reader.hasError())
{
qDebug()<<reader.errorString()<<"\n";
qDebug()<<"line number "<<reader.lineNumber()<<"\n";
}
xmlFile.close();
}
return true;
}

View File

@ -0,0 +1,28 @@
#ifndef ELEMENTHASHTABLE_H
#define ELEMENTHASHTABLE_H
#include <QObject>
#include <QXmlStreamReader>
#include <QString>
#include <QDebug>
#include <QFile>
#include <QHash>
#include "BasicElementInfo.h"
#include "subcontrolarea.h"
class ElementHashtable : public QObject
{
Q_OBJECT
public:
explicit ElementHashtable(QObject *parent = 0);
bool parse(const QString& xmlPath);
private:
bool child(QXmlStreamReader &reader);
QHash<QString,BasicElementInfo> eleHT;
signals:
public slots:
};
#endif // ELEMENTHASHTABLE_H

View File

@ -1,8 +1,11 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <elementhashtable.h>
#include <iostream>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
ElementHashtable eleReader;
eleReader.parse("D:/Project/佛山项目/佛山收资/exportmodel_zwyth/df8003/df8600/exportfiles/exportmodel_zwyth.xml");
std::cout<<"Finished."<<std::endl;
return a.exec(); return a.exec();
} }

View File

@ -0,0 +1,11 @@
#include "subcontrolarea.h"
SubControlArea::SubControlArea()
{
}
bool SubControlArea::parse(QXmlStreamReader &reader)
{
this->parseBasicInfo(reader);
return true;
}

View File

@ -0,0 +1,12 @@
#ifndef SUBCONTROLAREA_H
#define SUBCONTROLAREA_H
#include "BasicElementInfo.h"
#include <QXmlStreamReader>
class SubControlArea:public BasicElementInfo
{
public:
SubControlArea();
bool parse(QXmlStreamReader &reader);
};
#endif // SUBCONTROLAREA_H

View File

@ -5,6 +5,7 @@
#------------------------------------------------- #-------------------------------------------------
QT += core QT += core
QT +=xml
QT -= gui QT -= gui
@ -15,4 +16,16 @@ CONFIG -= app_bundle
TEMPLATE = app TEMPLATE = app
SOURCES += main.cpp SOURCES += main.cpp \
elementhashtable.cpp \
subcontrolarea.cpp \
BasicElementInfo.cpp
HEADERS += \
elementhashtable.h \
BasicElementInfo.h \
subcontrolarea.h
#release{
#DEFINES += QT_NO_DEBUG_OUTPUT
#}