-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanswer.cpp
More file actions
74 lines (64 loc) · 2.07 KB
/
Copy pathanswer.cpp
File metadata and controls
74 lines (64 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "answer.h"
Answer::Answer(const QString &xml)
{
this->code = ANSWER_ERROR;
this->result = QT_TR_NOOP(QString::fromUtf8("Получен некорректный ответ от сервера.")); // TODO:TR
QDomDocument doc;
if (!doc.setContent(xml)) return;
QDomElement docElem = doc.documentElement();
// переходим к парсингу
parseNode(docElem.firstChild());
}
Answer::Answer(AnswerCode code, const QString &result)
{
this->code = code;
this->result = result;
}
// код ответа
AnswerCode Answer::getCode()
{
return this->code;
}
// текстовая расшифровка кода
QString Answer::getResult()
{
return this->result;
}
// значение ответа
QString Answer::getValue(const QString &name)
{
return this->answer.value(name, "").toString();
}
// парсим xml
void Answer::parseNode(QDomNode node)
{
while (!node.isNull())
{
QDomElement e = node.toElement(); // пробуем преобразовать узел в элемент
if (!e.isNull())
{
// для корневого узла
if (e.tagName() == "result")
{
this->code = AnswerCode(e.attribute("code").toInt());
this->result = e.text();
}
// для комбинированных ответов сохраняем весь вывод
else if (e.tagName() == "journals" || e.tagName() == "classes" ||
e.tagName() == "courses" || e.tagName() == "teachers" ||
e.tagName() == "column_types")
{
QString data;
QTextStream out(&data);
node.save(out, 1);
this->answer.insert(e.tagName(), data);
}
// другие используем
else
{
this->answer.insert(e.tagName(), e.text());
}
}
node = node.nextSibling();
}
}