This repository was archived by the owner on Feb 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathXMLReader.h
More file actions
78 lines (64 loc) · 1.63 KB
/
XMLReader.h
File metadata and controls
78 lines (64 loc) · 1.63 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
#ifndef XML_READER_H_
#define XML_READER_H_
/*
* @brief The XMLReader class is responsible for dealing with libxml's api
* and constructing the XMLNode's objects representing the xml's nodes
*
* @author Filipe N. Felisbino
*
* */
#include <string>
#include <list>
#include <map>
#include <stdexcept>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "XMLNode.h"
using std::runtime_error;
using std::string;
using std::map;
using std::list;
class XMLReaderException : public runtime_error {
public:
XMLReaderException(const string &x) :
runtime_error(x) {};
};
struct XMLFileParser {
xmlDocPtr parse(const std::string &file) {
return xmlParseFile(file.c_str());
}
};
struct XMLMemoryParser {
xmlDocPtr parse(const std::string &xml) {
return xmlParseMemory(xml.c_str(), xml.size());
}
};
void getAttributes(xmlNode *node, map<string,string>&data);
void getChildren(xmlNode *node, XMLNode &result);
void populateNode(xmlNode* node, XMLNode &result);
template<class Parser>
class XMLReader {
public:
XMLReader(const string &o) : parser_(), doc_(NULL) {
doc_ = parser_.parse(o);
};
~XMLReader() {
if ( doc_ != NULL ) {
xmlFreeDoc(doc_);
}
};
void parse(list<XMLNode> &nodes) {
xmlNode *current = xmlDocGetRootElement(doc_);
while ( current != NULL ) {
XMLNode result;
populateNode(current, result);
nodes.push_back(result);
current = current->next;
}
};
bool findNode(const string&);
private:
Parser parser_;
xmlDocPtr doc_;
};
#endif // XML_READER_H_