-
Notifications
You must be signed in to change notification settings - Fork 0
/
msxml_wrap.h
192 lines (178 loc) · 5.13 KB
/
msxml_wrap.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
#pragma once
#include <MsXml2.h>
#define IF_FAILED_SET_LAST_ERROR_RETURN(expr, res) \
do { \
HRESULT hr = expr; \
if (FAILED(hr)) { \
::SetLastError(hr); \
return res; \
} \
} while (0)
// class XmlNode
class XmlNode {
public:
XmlNode(IXMLDOMNode *msxmlNode);
bool IsNull() const;
XmlNode GetFirstChild() const;
XmlNode GetNextSibling() const;
XmlNode GetParentNode() const;
bool AppendChild(XmlNode &child);
IXMLDOMNode *GetMSXMLNode() const;
protected:
CComPtr<IXMLDOMNode> msxmlNode_;
};
inline bool XmlNode::IsNull() const {
return msxmlNode_ == NULL;
}
inline XmlNode XmlNode::GetFirstChild() const {
ASSERT(!IsNull());
IXMLDOMNode *msxmlNode = NULL;
IF_FAILED_SET_LAST_ERROR_RETURN(msxmlNode_->get_firstChild(&msxmlNode), XmlNode(NULL));
return XmlNode(msxmlNode);
}
inline XmlNode XmlNode::GetNextSibling() const {
ASSERT(!IsNull());
IXMLDOMNode *msxmlNode = NULL;
IF_FAILED_SET_LAST_ERROR_RETURN(msxmlNode_->get_nextSibling(&msxmlNode), XmlNode(NULL));
return XmlNode(msxmlNode);
}
inline XmlNode XmlNode::GetParentNode() const {
ASSERT(!IsNull());
IXMLDOMNode *msxmlNode = NULL;
IF_FAILED_SET_LAST_ERROR_RETURN(msxmlNode_->get_parentNode(&msxmlNode), XmlNode(NULL));
return XmlNode(msxmlNode);
}
inline bool XmlNode::AppendChild(XmlNode &child) {
ASSERT(!IsNull());
IF_FAILED_SET_LAST_ERROR_RETURN(msxmlNode_->appendChild(child.msxmlNode_, NULL), false);
return true;
}
inline IXMLDOMNode *XmlNode::GetMSXMLNode() const {
return msxmlNode_;
}
// class XmlAttr
class XmlAttr {
public:
XmlAttr();
void SetValue(LPCTSTR value);
bool IsNull() const;
bool IsEmpty() const;
const CString &GetValue() const;
private:
bool isNull_;
CString value_;
};
inline void XmlAttr::SetValue(LPCTSTR value) {
isNull_ = false;
value_ = value;
}
inline bool XmlAttr::IsNull() const {
return isNull_;
}
inline bool XmlAttr::IsEmpty() const {
return value_.IsEmpty();
}
inline const CString &XmlAttr::GetValue() const {
return value_;
}
// class XmlElement
class XmlElement : public XmlNode {
public:
XmlElement(IXMLDOMElement *msxmlElm);
XmlElement(const XmlNode &node);
bool IsNull() const;
XmlAttr GetAttribute(LPCTSTR name);
bool GetAttribute(LPCTSTR name, CString *value);
CString GetTagName() const;
CString GetText() const;
bool SetAttribute(LPCTSTR name, LPCTSTR value);
IXMLDOMElement *GetMSXMLElement() const;
XmlElement &operator=(const XmlNode &node);
void Release();
XmlElement FindFirstElement(LPCTSTR name, int length);
private:
CComPtr<IXMLDOMElement> msxmlElm_;
};
inline bool XmlElement::IsNull() const {
return msxmlElm_ == NULL;
}
inline XmlAttr XmlElement::GetAttribute(LPCTSTR name) {
ASSERT(!IsNull());
XmlAttr res;
CComVariant value;
IF_FAILED_SET_LAST_ERROR_RETURN(msxmlElm_->getAttribute(CComBSTR(name), &value), res);
if (value.vt != VT_BSTR) {
::SetLastError(ERROR_DATATYPE_MISMATCH);
return res;
}
res.SetValue(value.bstrVal);
return res;
}
inline bool XmlElement::GetAttribute(LPCTSTR name, CString *value) {
ASSERT(!IsNull());
CComVariant var;
IF_FAILED_SET_LAST_ERROR_RETURN(msxmlElm_->getAttribute(CComBSTR(name), &var), false);
if (var.vt != VT_BSTR) {
::SetLastError(ERROR_DATATYPE_MISMATCH);
return false;
}
ASSERT(value != NULL);
*value = var.bstrVal;
return true;
}
inline CString XmlElement::GetTagName() const {
ASSERT(!IsNull());
CComBSTR name;
IF_FAILED_SET_LAST_ERROR_RETURN(msxmlElm_->get_tagName(&name), CString());
return CString(name);
}
inline CString XmlElement::GetText() const {
ASSERT(!IsNull());
CComBSTR text;
IF_FAILED_SET_LAST_ERROR_RETURN(msxmlElm_->get_text(&text), CString());
return CString(text);
}
inline bool XmlElement::SetAttribute(LPCTSTR name, LPCTSTR value) {
ASSERT(!IsNull());
IF_FAILED_SET_LAST_ERROR_RETURN(msxmlElm_->setAttribute(CComBSTR(name), CComVariant(value)), false);
return true;
}
inline IXMLDOMElement *XmlElement::GetMSXMLElement() const {
return msxmlElm_;
}
inline void XmlElement::Release() {
msxmlElm_.Release();
}
// class XmlDocument
class XmlDocument {
public:
XmlDocument();
bool IsNull() const;
bool Load(const CString &fileName);
bool Save(const CString &fileName);
XmlElement GetDocElement();
XmlElement CreateElement(const CString &tagName);
bool AppendChild(const XmlElement &elm);
private:
CComPtr<IXMLDOMDocument2> msxmlDoc_;
};
inline bool XmlDocument::IsNull() const {
return msxmlDoc_ == NULL;
}
inline XmlElement XmlDocument::GetDocElement() {
ASSERT(!IsNull());
IXMLDOMElement *msxmlElm = NULL;
IF_FAILED_SET_LAST_ERROR_RETURN(msxmlDoc_->get_documentElement(&msxmlElm), XmlElement(NULL));
return XmlElement(msxmlElm);
}
inline XmlElement XmlDocument::CreateElement(const CString &tagName) {
ASSERT(!IsNull());
IXMLDOMElement *msxmlElm = NULL;
IF_FAILED_SET_LAST_ERROR_RETURN(msxmlDoc_->createElement(CComBSTR(tagName), &msxmlElm), XmlElement(NULL));
return XmlElement(msxmlElm);
}
inline bool XmlDocument::AppendChild(const XmlElement &elm) {
ASSERT(!IsNull());
IF_FAILED_SET_LAST_ERROR_RETURN(msxmlDoc_->appendChild(elm.GetMSXMLElement(), NULL), false);
return true;
}