-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlNode.cpp
More file actions
159 lines (140 loc) · 3.85 KB
/
XmlNode.cpp
File metadata and controls
159 lines (140 loc) · 3.85 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "XmlNode.h"
void XmlNode::free()
{
childrens.clear();
}
XmlNode::~XmlNode()
{
free();
}
XmlNode* XmlNode::XmlReader(const std::string& content, size_t& i, size_t level)
{
std::cout << level << "REKURSIQ!!!\n";
bool flag = false;
std::string info = "";
if (content[i++] == '<')
{
while (content[i] != '>')
{
if (content[i] == ' ') break;
info += content[i++];
// std::cout << "PROBLEM: PERSON!" << info << std::endl;
}
if (content[i] == '>')
{
i++;
this->tag = info;
std::cout << "Tag: " << tag << '\n';
info = "";
}
if (content[i] == ' ')
{
i++;
this->tag = info;
std::cout << "Tag: " << tag << '\n';
info = "";
while (content[i] != '=')
{
info += content[i++];
}
i++;
this->attributes.setKey(info);
std::cout << "Key: " << attributes.getKey() << '\n';
info = "";
while (content[i] != '>')
{
//if (content[i] == ' ') break;
info += content[i++];
}
this->attributes.setValue(info);
std::cout << "Value: " << attributes.getValue() << '\n';
info = "";
i++;
}
std::cout << "Key: " << attributes.getKey() << '\n';
std::cout << "Value: " << attributes.getValue() << '\n';
if (content[i] != '<')
{
while (content[i] != '<')
{
info += content[i++];
}
flag = true;
this->text = info;
std::cout << "Text: " << text << '\n';
info = "";
}
if (content[i] == '<')
{
while (content[i + 1] != '/')
{
XmlNode* child = new XmlNode;
childrens.push_back(child->XmlReader(content, i, level + 1));
}
if (content[i + 1] == '/' && flag == true)
{
while (content[i] != '>') i++;
i++;
return this;
}
if (content[i + 1] == '/')
{
while (content[i] != '>') i++;
i++;
return this;
}
}
}
}
//size_t XmlNode::getChildrensSize() const
//{
// return childrens.size();
//}
XmlNode* XmlNode::searchID(const std::string& id) const
{
/*for (size_t i = 0; i < childrens.size(); i++)
{
if (id == childrens[i]->attributes.getValue()) return childrens[i];
}*/
std::cout << childrens.size();
return nullptr;
}
XmlNode* XmlNode::searchKey(const std::string& key) const
{
for (size_t i = 0; i < childrens.size(); i++)
{
if (childrens[i]->attributes.getKey() == key) return childrens[i];
}
return nullptr;
}
XmlNode* XmlNode::select(const std::string& id , const std::string& key)
{
XmlNode* ptr = searchID(id);
if (ptr == nullptr)
{
//std::cout << "This element does not exist!\n";
return nullptr;
}
if (key == "id") return ptr;
ptr = ptr->searchKey(key);
if (ptr == nullptr)
{
//std::cout << "This element does not exist!\n";
return nullptr;
}
return ptr;
}
void XmlNode::set(const std::string& id, const std::string& key, const std::string& value)
{
XmlNode* ptr = select(id, key);
ptr->attributes.setValue(value);
}
void XmlNode::children(const std::string& id) const
{
XmlNode* element = searchID(id);
for (size_t i = 0; i < element->childrens.size(); i++) // Cycles through all childrens
{
std::cout << element->childrens[i]->attributes.getKey() << " " << element[i].attributes.getValue() << "\n";
std::cout << '\n';
}
}