-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.cpp
More file actions
116 lines (98 loc) · 2.64 KB
/
Copy pathuser.cpp
File metadata and controls
116 lines (98 loc) · 2.64 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "user.h"
User::User(int id, QString login, QString password)
{
this->id = id;
this->login = login;
this->password = password;
this->name = "";
this->nameFull = "";
this->description = "";
this->userType = utUnknown;
}
User::User(const QString &xml)
{
this->login = login;
this->password = password;
this->name = "";
this->nameFull = "";
this->description = "";
this->userType = utUnknown;
// загружаем пользователя
if (xml != "") load(xml);
}
User::User(User *other)
{
this->id = other->id;
this->login = other->login;
this->password = other->password;
this->name = other->name;
this->nameFull = other->nameFull;
this->description = other->description;
this->userType = other->userType;
}
// загрузка пользователя из файла
bool User::load(QFile *file)
{
QDomDocument doc;
if (!doc.setContent(file->readAll())) return false;
QDomElement docElem = doc.documentElement();
// переходим к парсингу
parseNode(docElem);
return true;
}
// загрузка пользователя из строки
bool User::load(const QString &xml)
{
QDomDocument doc;
if (!doc.setContent(xml)) return false;
QDomElement docElem = doc.documentElement();
// переходим к парсингу
parseNode(docElem);
return true;
}
// вернуть идентификатор пользователя
int User::getId()
{
return id;
}
// установить идентификатор пользователя
void User::setId(int id)
{
this->id = id;
}
// вернуть имя пользователя
QString User::getName()
{
return this->name;
}
// парсим xml
void User::parseNode(QDomNode node)
{
while (!node.isNull())
{
QDomElement e = node.toElement(); // пробуем преобразовать узел в элемент
if (!e.isNull())
{
// для корневого узла
if (e.tagName() == "user")
{
this->id = e.attribute("id").toInt();
if (e.hasChildNodes()) parseNode(node.firstChild());
}
// другие используем
else if (e.tagName() == "name")
{
this->name = e.text();
}
else if (e.tagName() == "name_full")
{
this->nameFull = e.text();
}
else if (e.tagName() == "description")
{
this->description = e.text();
}
}
node = node.nextSibling();
}
}