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
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*
This is an example illustrating the use of the xml_parser component in
the dlib C++ Library.
This example simply reads in an xml file and prints the parsing events
to the screen.
*/
#include "dlib/xml_parser.h"
#include <iostream>
#include <fstream>
using namespace std;
using namespace dlib;
// ----------------------------------------------------------------------------------------
class doc_handler : public document_handler
{
/*
As the parser runs it generates events when it encounters tags and
data in an XML file. To be able to receive these events all you have to
do is make a class that inherits from dlib::document_handler and
implements its virtual methods. Then you simply associate an
instance of your class with the xml_parser.
So this class is a simple example document handler that just prints
all the events to the screen.
*/
public:
virtual void start_document (
)
{
cout << "parsing begins" << endl;
}
virtual void end_document (
)
{
cout << "Parsing done" << endl;
}
virtual void start_element (
const unsigned long line_number,
const std::string& name,
const dlib::attribute_list& atts
)
{
cout << "on line " << line_number << " we hit the <" << name << "> tag" << endl;
// print all the tag's attributes
atts.reset();
while (atts.move_next())
{
cout << "\tattribute: " << atts.element().key() << " = " << atts.element().value() << endl;
}
}
virtual void end_element (
const unsigned long line_number,
const std::string& name
)
{
cout << "on line " << line_number << " we hit the closing tag </" << name << ">" << endl;
}
virtual void characters (
const std::string& data
)
{
cout << "Got some data between tags and it is:\n" << data << endl;
}
virtual void processing_instruction (
const unsigned long line_number,
const std::string& target,
const std::string& data
)
{
cout << "on line " << line_number << " we hit a processing instruction with a target of '"
<< target << "' and data '" << data << "'" << endl;
}
};
// ----------------------------------------------------------------------------------------
class xml_error_handler : public error_handler
{
/*
This class handles error events that occur during parsing.
Just like the document_handler class above it just prints the events to the screen.
*/
public:
virtual void error (
const unsigned long line_number
)
{
cout << "There is a non-fatal error on line " << line_number << " in the file we are parsing." << endl;
}
virtual void fatal_error (
const unsigned long line_number
)
{
cout << "There is a fatal error on line " << line_number << " so parsing will now halt" << endl;
}
};
// ----------------------------------------------------------------------------------------
int main(int argc, char** argv)
{
// Check if the user entered an argument to this application.
if (argc != 2)
{
cout << "Please enter an xml file to parse on the command line" << endl;
return 1;
}
// Try to open the file given on the command line
ifstream fin(argv[1]);
if (!fin)
{
cout << "unable to open file: " << argv[1] << endl;
return 1;
}
// now make the xml parser and our document and error handlers
xml_parser::kernel_1a_c parser;
doc_handler dh;
xml_error_handler eh;
// now associate the handlers with the parser and tell it to parse
parser.add_document_handler(dh);
parser.add_error_handler(eh);
parser.parse(fin);
}