-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathxml.h
207 lines (173 loc) · 5.63 KB
/
xml.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* Part of WinLamb - Win32 API Lambda Library
* https://github.com/rodrigocfd/winlamb
* Copyright 2017-present Rodrigo Cesar de Freitas Dias
* This library is released under the MIT License
*/
#pragma once
#include <string>
#include "com.h"
#include "insert_order_map.h"
#include <MsXml2.h>
#pragma comment(lib, "msxml2.lib")
namespace wl {
// XML wrapper class to MSXML2 Windows library.
class xml final {
public:
// A single XML node.
class node final {
public:
std::wstring name;
std::wstring value;
insert_order_map<std::wstring, std::wstring> attrs;
std::vector<node> children;
void clear() noexcept {
this->name.clear();
this->value.clear();
this->attrs.clear();
this->children.clear();
}
std::vector<std::reference_wrapper<node>> children_by_name(const wchar_t* elemName) {
std::vector<std::reference_wrapper<node>> nodeBuf;
for (node& node : this->children) {
if (!lstrcmpiW(node.name.c_str(), elemName)) { // case-insensitive match
nodeBuf.emplace_back(node);
}
}
return nodeBuf;
}
std::vector<std::reference_wrapper<node>> children_by_name(const std::wstring& elemName) {
return this->children_by_name(elemName.c_str());
}
node* first_child_by_name(const wchar_t* elemName) noexcept {
for (node& node : this->children) {
if (!lstrcmpiW(node.name.c_str(), elemName)) { // case-insensitive match
return &node;
}
}
return nullptr; // not found
}
node* first_child_by_name(const std::wstring& elemName) noexcept {
return this->first_child_by_name(elemName.c_str());
}
};
private:
com::lib _comLib{com::lib::init::LATER};
public:
// Root node of this XML document.
node root;
xml() = default;
xml(xml&& other) noexcept : root{std::move(other.root)} { }
xml(const wchar_t* str) { this->parse(str); }
xml(const std::wstring& str) : xml(str.c_str()) { }
xml& operator=(xml&& other) noexcept {
this->root.clear();
this->root = std::move(other.root);
return *this;
}
xml& parse(const wchar_t* str) {
this->_comLib.initialize(); // init COM library, if not yet
this->root.clear();
// Create COM object for XML document.
com::ptr<IXMLDOMDocument2> doc;
doc.co_create_instance(CLSID_DOMDocument30, IID_IXMLDOMDocument);
doc->put_async(FALSE);
// Parse the XML string.
VARIANT_BOOL vb = FALSE;
com::check_hr(
doc->loadXML(static_cast<BSTR>(const_cast<wchar_t*>(str)), &vb),
"IXMLDOMDocument::loadXML failed");
// Get document element and root node from XML.
com::ptr<IXMLDOMElement> docElem;
com::check_hr(
doc->get_documentElement(&docElem),
"IXMLDOMDocument::get_documentElement failed");
com::ptr<IXMLDOMNode> rootNode;
docElem.query_interface(IID_IXMLDOMNode, &rootNode);
this->root = _build_node(rootNode); // recursive, the whole tree is loaded into memory
return *this;
}
xml& parse(const std::wstring& str) {
return this->parse(str.c_str());
}
private:
static xml::node _build_node(com::ptr<IXMLDOMNode>& xmlDomNode) {
xml::node ret;
// Get node name.
com::bstr bstrName;
xmlDomNode->get_nodeName(&bstrName);
ret.name = bstrName.c_str();
// Parse attributes of node, if any.
ret.attrs = _read_attrs(xmlDomNode);
// Process children, if any.
VARIANT_BOOL vb = FALSE;
xmlDomNode->hasChildNodes(&vb);
if (vb) {
com::ptr<IXMLDOMNodeList> nodeList;
xmlDomNode->get_childNodes(&nodeList);
ret.children.reserve(_count_child_nodes(nodeList));
int childCount = 0;
long totalCount = 0;
nodeList->get_length(&totalCount);
for (long i = 0; i < totalCount; ++i) {
com::ptr<IXMLDOMNode> child;
nodeList->get_item(i, &child);
// Node can be text or an actual child node.
DOMNodeType type = NODE_INVALID;
child->get_nodeType(&type);
if (type == NODE_TEXT) {
com::bstr bstrText;
xmlDomNode->get_text(&bstrText);
ret.value.append(bstrText.c_str());
} else if (type == NODE_ELEMENT) {
ret.children.emplace_back(_build_node(child)); // recursively
} else {
// (L"Unhandled node type: %d.\n", type);
}
}
} else {
// Assumes that only a leaf node can have text.
com::bstr bstrText;
xmlDomNode->get_text(&bstrText);
ret.value = bstrText.c_str();
}
return ret;
}
static insert_order_map<std::wstring, std::wstring> _read_attrs(com::ptr<IXMLDOMNode>& xmlnode) {
// Read attribute collection.
com::ptr<IXMLDOMNamedNodeMap> attrs;
xmlnode->get_attributes(&attrs);
long attrCount = 0;
attrs->get_length(&attrCount);
insert_order_map<std::wstring, std::wstring> ret;
ret.reserve(attrCount);
for (long i = 0; i < attrCount; ++i) {
com::ptr<IXMLDOMNode> attr;
attrs->get_item(i, &attr);
DOMNodeType type = NODE_INVALID;
attr->get_nodeType(&type);
if (type == NODE_ATTRIBUTE) {
com::bstr bstrName;
attr->get_nodeName(&bstrName); // get attribute name
com::variant variNodeVal;
attr->get_nodeValue(&variNodeVal); // get attribute value
ret[bstrName.c_str()] = variNodeVal.get_str(); // add hash entry
}
}
return ret;
}
static int _count_child_nodes(com::ptr<IXMLDOMNodeList>& nodeList) noexcept {
int childCount = 0;
long totalCount = 0;
nodeList->get_length(&totalCount); // includes text and actual element nodes
for (long i = 0; i < totalCount; ++i) {
com::ptr<IXMLDOMNode> child;
nodeList->get_item(i, &child);
DOMNodeType type = NODE_INVALID;
child->get_nodeType(&type);
if (type == NODE_ELEMENT) ++childCount;
}
return childCount;
}
};
}//namespace wl