00001 #include "xmlReader.h"
00002
00003 XmlReader::XmlReader()
00004 {
00005 filename = "xml_examples/ebel2010.xml";
00006 }
00007
00008 XmlReader::XmlReader(QString filename)
00009 : filename(filename)
00010 {
00011 }
00012
00013 Tournament* XmlReader::read (void)
00014 {
00015 return this->read("");
00016 }
00017
00018 Tournament* XmlReader::read(QString fileN)
00019 {
00020 Tournament* tournament;
00021
00022 if(fileN!="")
00023 {
00024 filename = fileN;
00025 qDebug("fileN!=");
00026 }
00027
00028 QDomDocument doc( "Tournament" );
00029 QFile file( filename );
00030 if( !file.open( IO_ReadOnly ) ) {
00031 qDebug("IO_ReadOnly");
00032 }
00033 if( !doc.setContent( &file ) )
00034 {
00035 file.close();
00036 }
00037 file.close();
00038
00039 QDomElement root = doc.documentElement();
00040 if( root.tagName() != "tournament" )
00041 {
00042 throw 1;
00043 qDebug("Wrong XML file format");
00044 }
00045
00046 QDomElement e = root.toElement();
00047 QString title = e.attribute("name","");
00048 qDebug(title.toLatin1());
00049
00050
00051 QDomNode firstC = root.firstChild();
00052 QVector<int> ids;
00053 QVector<QString> names;
00054 QVector<QString> infos;
00055 QVector<QString> icons;
00056 QVector<int> picks;
00057 int counter = 0;
00058
00059
00060 while( !firstC.isNull() )
00061 {
00062 QDomElement e = firstC.toElement();
00063 if( !e.isNull() )
00064 {
00065 if( e.tagName() == "teams" )
00066 {
00067
00068 QDomNode inner = e.firstChild();
00069 while(!inner.isNull())
00070 {
00071 QDomElement next = inner.toElement();
00072 if(!next.isNull()) {
00073
00074 counter++;
00075
00076 ids.append(next.attribute("id","").toInt());
00077
00078 QDomNodeList nameList = next.elementsByTagName("name");
00079 QDomNodeList infoList = next.elementsByTagName("info");
00080 QDomNodeList iconList = next.elementsByTagName("icon");
00081 QDomNodeList pickList = next.elementsByTagName("pick");
00082
00083 for(int i=0; i<nameList.count(); i++)
00084 {
00085
00086 names.append(nameList.at(i).toElement().text());
00087 infos.append(infoList.at(i).toElement().text());
00088 icons.append(iconList.at(i).toElement().text());
00089 picks.append(pickList.at(i).toElement().text().toInt());
00090
00091 }
00092 }
00093 inner = inner.nextSibling();
00094 }
00095
00096 tournament = new Tournament(counter,title);
00097 QVector<Team*> teams = tournament->getTeams();
00098
00099 for(int i=0; i<teams.size(); i++)
00100 {
00101 teams[i]->setName(names.at(i));
00102 qDebug(icons.at(i).toLatin1());
00103 teams[i]->setIconFile(icons.at(i));
00104 teams[i]->setPick(Team::getPickForInt(picks.at(i)));
00105 }
00106 tournament->setTeams(teams);
00107
00108 }
00109
00110 ids.clear();
00111 QVector<int> teams1;
00112 QVector<int> teams2;
00113 QVector<int> results1;
00114 QVector<int> results2;
00115 QVector<QString> dates;
00116 QVector<QString> descr;
00117
00118 if(e.tagName() == "games")
00119 {
00120 QDomNode inner = e.firstChild();
00121 while(!inner.isNull())
00122 {
00123 QDomElement next = inner.toElement();
00124 if(!next.isNull()) {
00125
00126 ids.append(next.attribute("id","").toInt());
00127 QDomNodeList team1List = next.elementsByTagName("team1");
00128 QDomNodeList team2List = next.elementsByTagName("team2");
00129 QDomNodeList resultList = next.elementsByTagName("result");
00130 QDomNodeList dateList = next.elementsByTagName("date");
00131 QDomNodeList descrList = next.elementsByTagName("descr");
00132
00133 for(int i=0; i<team1List.count(); i++)
00134 {
00135
00136 teams1.append(team1List.at(i).toElement().text().toInt());
00137 teams2.append(team2List.at(i).toElement().text().toInt());
00138 QString temp = resultList.at(i).toElement().text();
00139 results1.append(temp.split(":").at(0).toInt());
00140 results2.append(temp.split(":").at(1).toInt());
00141 dates.append(dateList.at(i).toElement().text());
00142 descr.append(descrList.at(i).toElement().text());
00143
00144 }
00145 }
00146
00147 inner = inner.nextSibling();
00148 }
00149
00150 QVector<Team*> teams = tournament->getTeams();
00151 Game* game;
00152 Team* t1;
00153 Team* t2;
00154
00155 for(int i=0; i<teams1.size(); i++)
00156 {
00157 game = tournament->getGameByID(ids[i]);
00158 for(int j=0; j<teams.size(); j++)
00159 {
00160 if(teams[j]->getId()==teams1.at(i)) {
00161 t1 = teams.at(j);
00162 }
00163 if(teams[j]->getId()==teams2.at(i)) t2 = teams.at(j);
00164 }
00165 game->setTeam(1, t1);
00166 game->setTeam(2, t2);
00167
00168 int result[2];
00169 result[0]=results1.at(i);
00170 result[1]=results2.at(i);
00171
00172 game->setResult(result);
00173
00174 }
00175 }
00176
00177 }
00178
00179 firstC = firstC.nextSibling();
00180 }
00181 return tournament;
00182 }