-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDOMParser.cpp
More file actions
69 lines (64 loc) · 1.58 KB
/
Copy pathDOMParser.cpp
File metadata and controls
69 lines (64 loc) · 1.58 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
#include "DOMParser.h"
TagElement* DOMParser::StartParsing(std::string FileName) {
std::string TmpS;
std::smatch matches;
std::regex StartTag("<[a-zA-z]+>");
std::regex EndTag("</[a-zA-z]+>");
std::regex Ñontent("[0-9]+");
std::ifstream myfile;
myfile.open(FileName);
getline(myfile, TmpS);
while (getline(myfile, TmpS))
{
std::regex_search(TmpS, matches, StartTag);
if (matches.size() != 0) {
FoundStart(matches[0]);
}
regex_search(TmpS, matches, Ñontent);
if (matches.size() != 0) {
FoundÑontent(matches[0]);
}
regex_search(TmpS, matches, EndTag);
if (matches.size() != 0) {
FoundEnd(matches[0]);
}
}
if (Tags.size() != 0) {
throw new ExeptionHandler("Some tags aren't closed");
};
myfile.close();
return Root;
};
void DOMParser::FoundStart(std::string Tag)
{
std::smatch TagName;
std::regex RegName("[a-zA-z]+");
regex_search(Tag, TagName, RegName);
Tags.push(TagName[0]);
if (!Root) {
Root = new TagElement(Root,TagName[0]);
CurrentElement = Root;
}
else {
TagElement* TMP = new TagElement(CurrentElement, TagName[0]);
CurrentElement->children.push_back(TMP);
CurrentElement = TMP;
}
};
void DOMParser::FoundEnd(std::string Tag)
{
std::smatch TagName;
std::regex RegName("[a-zA-z]+");
regex_search(Tag, TagName, RegName);
if (Tags.top() == TagName[0]) {
Tags.pop();
CurrentElement = CurrentElement->parent;
}
else
{
throw new ExeptionHandler("Wrong tag order");
}
};
void DOMParser::FoundÑontent(std::string Ñontent) {
CurrentElement->InnerText = Ñontent;
};