Skip to content

Instantly share code, notes, and snippets.

@mavelar
Last active May 10, 2016 19:22
Show Gist options
  • Select an option

  • Save mavelar/940f1848b2e481bc8cd6 to your computer and use it in GitHub Desktop.

Select an option

Save mavelar/940f1848b2e481bc8cd6 to your computer and use it in GitHub Desktop.
public struct function xmlToJSON(required xml doc) {
var s = {};
var x = arguments.doc
if(xmlGetNodeType(x) == "DOCUMENT_NODE") {
s[structKeyList(x)] = xmlToJson(x[structKeyList(x)]);
} else {
if(structKeyExists(x, "xmlAttributes") && !structIsEmpty(x.xmlAttributes)) {
for(var item in x.xmlAttributes) {
s[item] = x.xmlAttributes[item];
}
}
if (structKeyExists(x, "xmlText") && len(trim(x.xmlText))){
return replace(replace(x.xmlText, Chr(10), "", "all"), chr(09), "", "all");
}
if(structKeyExists(x, "xmlChildren") && arrayLen(x.xmlChildren)) {
for(var i=1; i<=arrayLen(x.xmlChildren); i++) {
if(structKeyExists(s, x.xmlchildren[i].xmlname)) {
if(!isArray(s[x.xmlChildren[i].xmlname])) {
var temp = s[x.xmlchildren[i].xmlname];
s[x.xmlchildren[i].xmlname] = [temp];
}
arrayAppend(s[x.xmlchildren[i].xmlname], xmlToJson(x.xmlChildren[i]));
} else {
//before we parse it, see if simple
if(structKeyExists(x.xmlChildren[i], "xmlChildren") && arrayLen(x.xmlChildren[i].xmlChildren)) {
s[x.xmlChildren[i].xmlName] = xmlToJson(x.xmlChildren[i]);
} else if(structKeyExists(x.xmlChildren[i],"xmlAttributes") && !structIsEmpty(x.xmlChildren[i].xmlAttributes)) {
s[x.xmlChildren[i].xmlName] = xmlToJson(x.xmlChildren[i]);
} else {
s[x.xmlChildren[i].xmlName] = replace(replace(x.xmlChildren[i].xmlText, Chr(10), "", "all"), chr(09), "", "all");
}
}
}
}
}
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment