00001 #include "xmlWriter.h"
00002
00003 XmlWriter::XmlWriter()
00004 {
00005 filename = "default.xml";
00006 }
00007
00008 XmlWriter::XmlWriter(QString filename)
00009 : filename(filename)
00010 {
00011 }
00012
00013 bool XmlWriter::write (Tournament* tournament)
00014 {
00015 bool correct=false;
00016
00017 QFile file( filename );
00018 qDebug(filename.toLatin1());
00019 if( !file.open( IO_ReadWrite ) )
00020 {
00021 qDebug("Loading Failed to load file." );
00022 return correct;
00023 }
00024
00025 QDomDocument doc("MyTournament");
00026 QDomElement root = doc.createElement("tournament");
00027 root.setAttribute("name", tournament->getTitle());
00028 doc.appendChild(root);
00029
00030
00031 QDomElement tag = doc.createElement("teams");
00032 root.appendChild(tag);
00033 QVector<Team*> teams = tournament->getTeams();
00034
00035 QDomElement temp;
00036 QDomElement inner;
00037 QDomText t;
00038 for(int i=0; i<teams.size(); i++)
00039 {
00040 temp = doc.createElement("team");
00041 temp.setAttribute("id",QString::number(teams[i]->getId()));
00042
00043
00044 inner = doc.createElement("name");
00045 t = doc.createTextNode(teams[i]->getName());
00046 inner.appendChild(t);
00047 temp.appendChild(inner);
00048
00049
00050 inner = doc.createElement("info");
00051 t = doc.createTextNode(teams[i]->getInfo());
00052 inner.appendChild(t);
00053 temp.appendChild(inner);
00054
00055
00056 inner = doc.createElement("icon");
00057 t = doc.createTextNode(teams[i]->getIconFileName());
00058 inner.appendChild(t);
00059 temp.appendChild(inner);
00060
00061
00062 inner = doc.createElement("pick");
00063 t = doc.createTextNode(QString::number(teams[i]->getPick()));
00064 inner.appendChild(t);
00065 temp.appendChild(inner);
00066
00067 tag.appendChild(temp);
00068 }
00069
00070
00071 tag = doc.createElement("games");
00072 root.appendChild(tag);
00073 QVector<Game*> games = tournament->getGames();
00074
00075 for(int i=0; i<games.size(); i++)
00076 {
00077 temp = doc.createElement("game");
00078 temp.setAttribute("id",QString::number(games[i]->getID()));
00079
00080
00081 inner = doc.createElement("team1");
00082 t = doc.createTextNode(QString::number(games[i]->getTeam1()));
00083 inner.appendChild(t);
00084 temp.appendChild(inner);
00085
00086
00087 inner = doc.createElement("team2");
00088 t = doc.createTextNode(QString::number(games[i]->getTeam2()));
00089 inner.appendChild(t);
00090 temp.appendChild(inner);
00091
00092
00093 inner = doc.createElement("result");
00094 Game::Result r = games[i]->getResult();
00095 QString res;
00096 res.append(QString::number(r.score[0]));
00097 res.append(":");
00098 res.append(QString::number(r.score[1]));
00099 t = doc.createTextNode(res);
00100 inner.appendChild(t);
00101 temp.appendChild(inner);
00102
00103
00104 inner = doc.createElement("date");
00105 t = doc.createTextNode(games[i]->getDate());
00106 inner.appendChild(t);
00107 temp.appendChild(inner);
00108
00109
00110 inner = doc.createElement("descr");
00111 t = doc.createTextNode(games[i]->getDescr());
00112 inner.appendChild(t);
00113 temp.appendChild(inner);
00114
00115 tag.appendChild(temp);
00116 }
00117
00118
00119 QString xml = doc.toString();
00120 file.write("<?xml version='1.0' encoding='utf-8'?>");
00121 file.write(xml.toLatin1());
00122
00123 file.flush();
00124 file.close();
00125
00126
00127 return correct;
00128 }