-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathsearchXML.nut
48 lines (42 loc) · 1.44 KB
/
searchXML.nut
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
// Finds the value inside the xml tag at the specified path
//
// Parameters:
// xml string containing the XML to search
// path array representing the path to the tag
// cb optional callback that the result will be passed to
// Return: the value inside the tag as a string (or null if a cb is provided)
function searchXML(xml, path, cb=null) {
local v = split(path[0], "][");
local tag = v[0];
local openTagRegex = regexp2(@"<" + tag + "" + "[^>]*>");
local closeTagRegex = regexp2(@"</" + tag + ">");
local openTag;
local closeTag;
if (v.len() > 1) {
local index = v[1].tointeger();
local startOffset = 0;
for (local i = 0; i < index; i++) {
openTag = openTagRegex.search(xml, startOffset);
closeTag = closeTagRegex.search(xml, startOffset);
startOffset = closeTag.end;
}
} else {
openTag = openTagRegex.search(xml);
closeTag = closeTagRegex.search(xml);
}
if (path.len() > 1) {
local subXML = xml.slice(openTag.end, closeTag.begin);
if (cb == null) {
return searchXML(subXML, path.slice(1));
} else {
cb(searchXML(subXML, path.slice(1)));
}
} else {
local content = xml.slice(openTag.end, closeTag.begin);
if (cb == null) {
return content;
} else {
cb(content);
}
}
}