-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
75 lines (66 loc) · 1.51 KB
/
Copy pathmain.cpp
File metadata and controls
75 lines (66 loc) · 1.51 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
#include "xstream.h"
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
#include <sys/types.h>
#else
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#endif
void test1()
{
#if 1
std::string xml = R"---(<hoge><fuga foo='bar'>Hello, <![CDATA[world]]></fuga></hoge>)---";
#else
#ifdef _WIN32
char const *path = "Z:/_tmp/test.xml";
#else
char const *path = "/tmp/test.xml";
#endif
std::vector<char> data;
int fd = open(path, O_RDONLY);
if (fd != -1) {
struct stat st;
if (fstat(fd, &st) == 0) {
data.resize(st.st_size);
read(fd, data.data(), data.size());
}
}
if (data.empty()) return 1;
std::string_view xml(data.data(), data.size());
#endif
xstream::Reader x(xml);
while (x.next()) {
if (x.is_characters()) {
auto v = x.characters().decode();
std::string_view sv(v.data(), v.size());
printf("Characters: %s\n", std::string(sv).c_str());
} else if (x.is_start_element()) {
printf("StartElement: %s\n", x.name().c_str());
} else if (x.is_end_element()) {
printf("EndElement: %s @ %s\n", x.name().c_str(), x.path().c_str());
}
}
printf("done\n");
}
void test2()
{
std::string xml = R"---(<hoge>abc<fuga foo='bar'>def</fuga>ghi</hoge>)---";
xstream::Reader r(xml);
while (r.next()) {
if (r.is_end_element()) {
printf("%s: %s\n", r.path().c_str(), r.text().c_str());
for (auto const &a : r.attributes()) {
printf("%s: %s\n", a.first.c_str(), std::string(a.second).c_str());
}
}
}
}
int main()
{
test2();
return 0;
}