forked from acampb311/xml2struct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml2struct.cpp
253 lines (206 loc) · 7.98 KB
/
xml2struct.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <algorithm>
#include <string.h>
#include <vector>
#include "pugixml.hpp"
#include "mex.h"
using namespace pugi;
const char* node_types[] = { "null", "document", "element", "pcdata", "cdata", "comment", "pi", "declaration" };
bool hasChildNodes(pugi::xml_node& node)
{
return node.first_child() == NULL ? false : true;
}
std::vector<pugi::xml_node> getChildNodes(pugi::xml_node& node)
{
std::vector<pugi::xml_node> childNodes;
node = node.first_child();
while (node != NULL)
{
childNodes.push_back(node);
node = node.next_sibling();
}
return childNodes;
}
std::vector<std::string> getDistinctNodeNames(std::vector<std::string> nodes)
{
std::vector<std::string> distinctNames;
for (int i = 0; i < nodes.size(); i++)
{
if (!(std::find(distinctNames.begin(), distinctNames.end(), nodes.at(i)) != distinctNames.end()))
{
distinctNames.push_back(nodes.at(i));
}
}
return distinctNames;
}
mxArray *parseAttributes(pugi::xml_node& node)
{
mxArray *attributes = NULL;
pugi::xml_attribute attr = node.first_attribute();
int pos;
if (attr != NULL)
{
std::string tempName = attr.name();
while ((pos = tempName.find(":")) != std::string::npos)
{
tempName.replace(pos, 1, "_colon_");
}
while ((pos = tempName.find(".")) != std::string::npos)
{
tempName.replace(pos, 1, "_dot_");
}
while ((pos = tempName.find("-")) != std::string::npos)
{
tempName.replace(pos, 1, "_dash_");
}
const char *attributeName[1] = {tempName.c_str()};
mxArray *temp = mxCreateStructMatrix(1, 1, 1, attributeName);
mxArray *attrValue = mxCreateString(attr.value());
mxSetField(temp, 0, attributeName[0], attrValue);
for (attr = attr.next_attribute(); attr; attr = attr.next_attribute())
{
std::string tempName = attr.name();
while ((pos = tempName.find(":")) != std::string::npos)
{
tempName.replace(pos, 1, "_colon_");
}
while ((pos = tempName.find(".")) != std::string::npos)
{
tempName.replace(pos, 1, "_dot_");
}
while ((pos = tempName.find("-")) != std::string::npos)
{
tempName.replace(pos, 1, "_dash_");
}
const char *attrFieldName = tempName.c_str();
mxArray *attrValue = mxCreateString(attr.value());
mxAddField(temp, attrFieldName);
mxSetField(temp, 0, attrFieldName, attrValue);
}
attributes = temp;
}
return attributes;
}
mxArray* parseChildNodes(pugi::xml_node& node)
{
mxArray *children = NULL;
if (hasChildNodes(node))
{
mxArray *attributes = parseAttributes(node);
std::vector<std::string> distinctNames;
std::vector<std::string> allChildNodeNames;
std::vector<pugi::xml_node> childNodes;
int numChildNodes;
childNodes = getChildNodes(node);
numChildNodes = childNodes.size();
for (int i = 0; i < numChildNodes; i++)
{
allChildNodeNames.push_back(childNodes.at(i).name());
}
distinctNames = getDistinctNodeNames(allChildNodeNames);
/* Patch for bypassing the variable-length arrays problems of modern C++ compilers */
std::vector<const char*> distinctChildNodeNames;
std::transform(distinctNames.begin(), distinctNames.end(), std::back_inserter(distinctChildNodeNames), [](const std::string & str) {
// initialize empty char array
char *output = new char[str.size()+1];
std::strcpy(output, str.c_str());
return output;
});
std::vector<std::string> processedNames;
children = mxCreateStructMatrix(1, 1, distinctNames.size(), &distinctChildNodeNames[0]);
for (int idx = 0; idx < childNodes.size(); idx++)
{
pugi::xml_node theChild = childNodes.at(idx);
std::string type = node_types[theChild.type()];
std::string temp = theChild.name();
std::string val = theChild.value();
const char *namey[1] = {};
namey[0] = temp.c_str();
mxArray *glhf = mxGetField(children, 0, namey[0]);
int indexOfMatchingItem = mxGetFieldNumber(children, namey[0]);
if (!(strcmp(type.c_str(), "pcdata") == 0) && !(strcmp(type.c_str(), "comment") == 0) && !(strcmp(type.c_str(), "cdata") == 0))
{
//XML allows the same elements to be defined multiple times, put each in a different cell
if (std::find(processedNames.begin(), processedNames.end(), temp) != processedNames.end())
{
if ( glhf != NULL )
{
if (!mxIsCell(glhf))
{
mxArray *temp = glhf;
glhf = mxCreateCellMatrix(1, 2);
mxSetCell(glhf, 0, temp);
mxSetCell(glhf, 1, parseChildNodes(theChild));
mxSetCell(children, indexOfMatchingItem, glhf);
}
else
{
int numberItemsInCell = mxGetN(glhf);
mxArray *temp = glhf;
glhf = mxCreateCellMatrix(1, numberItemsInCell + 1);
for (int i = 0; i < numberItemsInCell; i++)
{
mxSetCell(glhf, i, mxGetCell(temp, i));
}
mxSetCell(glhf, numberItemsInCell, parseChildNodes(theChild));
mxSetCell(children, indexOfMatchingItem, glhf);
}
}
}
//add previously unknown (new) element to the structure
else
{
mxSetCell(children, indexOfMatchingItem, parseChildNodes(theChild));
}
processedNames.push_back(temp);
}
else
{
const char *typeFieldNames[1] = {"Text"};
std::string value = theChild.value();
mxArray *matValue = mxCreateString(value.c_str());
if (strcmp(type.c_str(), "cdata") == 0)
{
typeFieldNames[0] = "CDATA";
}
else if (strcmp(type.c_str(), "comment") == 0)
{
typeFieldNames[0] = "Comment";
}
children = mxCreateStructMatrix(1, 1, 1, typeFieldNames);
mxSetFieldByNumber(children, 0, 0, matValue);
processedNames.push_back(temp);
}
}
if (attributes != NULL)
{
const char *attrFieldName = "Attributes";
mxAddField(children, attrFieldName);
mxSetField(children, 0, attrFieldName, attributes);
}
}
return children;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
char *input_buf;
size_t buflen;
if(nrhs!=1)
mexErrMsgIdAndTxt( "MATLAB:revord:invalidNumInputs", "One input required.");
if ( mxIsChar(prhs[0]) != 1)
mexErrMsgIdAndTxt( "MATLAB:revord:inputNotString", "Input must be a string.");
buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
input_buf = mxArrayToString(prhs[0]);
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(input_buf);
if (!result)
{
mexPrintf("Error description: %s\n", result.description());
mexErrMsgIdAndTxt( "MATLAB:revord:fileIssue", "There was an issue with the given file.");
}
else
{
pugi::xml_node topNode = doc;
mxArray *vin1 = parseChildNodes(topNode);
plhs[0] = vin1;
}
}