2 #include <QNetworkRequest>
3 #include <QNetworkReply>
11 #include <QApplication>
12 #include "xmlreader.h"
15 /*! @brief Constructor, connects object to GUI
17 XmlReader::XmlReader(Ui_MainWindow* myMainWindow)
24 XmlReader::~XmlReader()
29 QString XmlReader::errorString() const
31 return QObject::tr("%1\nLine %2, column %3")
32 .arg(xmlreader.errorString())
33 .arg(xmlreader.lineNumber())
34 .arg(xmlreader.columnNumber());
38 /*! @brief parsing xml file that is sent by server over network.
39 * Shows received information on screen and parsed information on command line.
40 * @note Partly commented for git.
41 * @todo (Development: Seems to omit element tags and some values when reading from QFile. Why? This used
42 * earlier with QNetworkReply printed all xml contents.)
43 * @todo Remove command line output and use final GUI widget names.
44 * @todo Replace using of QFile to QNetworkReply.
46 void XmlReader::xmlRead(QIODevice *device)
47 //void XmlReader::xmlRead(QNetworkReply *device)
49 qDebug() << "_xmlRead";
51 xmlreader.addData(device->readAll());
52 //Or: View webpage contents on textEdit by adding all data to xml stream reader,
53 //since readAll() empties the buffer
54 /*QByteArray readAllArr = device->readAll();
55 ui->textEditXml->append(readAllArr);
56 xmlreader.addData(readAllArr);*/
58 //Go trough the xml document
59 while(!xmlreader.atEnd())
63 //Check if this element is starting element
64 if(xmlreader.isStartElement())
66 if(xmlreader.name() == "place")
68 qDebug() << xmlreader.name();
70 if(xmlreader.name() == "date")
72 qDebug() << xmlreader.name();
74 if(xmlreader.name() == "time")
76 qDebug() << xmlreader.name();
78 if(xmlreader.name() == "result")
80 //Two consequent start elements
81 if(xmlreader.readNextStartElement())
83 if(xmlreader.name() == "speed")
86 QXmlStreamAttributes attr = xmlreader.attributes();
87 speed = (attr.value("value").toString() + " " + attr.value("unit").toString());
88 //ui->lineEditSpeed->setText(speed);
89 qDebug() << xmlreader.name() << speed;
93 if(xmlreader.name() == "distance")
96 QXmlStreamAttributes attr = xmlreader.attributes();
97 distance = (attr.value("value").toString() + " " + attr.value("unit").toString());
98 //ui->lineEditDist->setText(distance);
99 qDebug() << xmlreader.name() << distance;
101 if(xmlreader.name() == "acceleration")
103 QString acceleration;
104 QXmlStreamAttributes attr = xmlreader.attributes();
105 acceleration = (attr.value("value").toString()+ " " + attr.value("unit").toString());
106 //ui->lineEditAccel->setText(acceleration);
107 qDebug() << xmlreader.name() << acceleration;
113 /*! @brief A function used tmp in development.
114 * @note Partly harcoded and commented for git.
116 void XmlReader::xmlShow()
118 //QString filename = ui->lineEditFile->text();
119 QString filename = "xmlfile.xml";
120 QFile file(filename);
122 if (!file.open(QFile::ReadOnly))
124 /*QMessageBox::warning(this->ui->centralWidget,
125 tr("QXmlStream Bookmarks"),
126 tr("Cannot write file %1:\n%2.")
127 .arg(file.fileName())
128 .arg(file.errorString()));*/
129 qDebug() << "_xmlShow fail";