Skip to content

Instantly share code, notes, and snippets.

@cfjedimaster
Created January 20, 2013 18:20
Show Gist options
  • Save cfjedimaster/4580449 to your computer and use it in GitHub Desktop.
Save cfjedimaster/4580449 to your computer and use it in GitHub Desktop.
function xmlToStruct(xml x) {
var s = {};
if(xmlGetNodeType(x) == "DOCUMENT_NODE") {
s[structKeyList(x)] = xmlToStruct(x[structKeyList(x)]);
}
if(structKeyExists(x, "xmlAttributes") && !structIsEmpty(x.xmlAttributes)) {
s.attributes = {};
for(var item in x.xmlAttributes) {
s.attributes[item] = x.xmlAttributes[item];
}
}
if(structKeyExists(x, "xmlText") && len(trim(x.xmlText))) {
s.value = x.xmlText;
}
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], xmlToStruct(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] = xmlToStruct(x.xmlChildren[i]);
} else if(structKeyExists(x.xmlChildren[i],"xmlAttributes") && !structIsEmpty(x.xmlChildren[i].xmlAttributes)) {
s[x.xmlChildren[i].xmlName] = xmlToStruct(x.xmlChildren[i]);
} else {
writeoutput("debug - did simple #x.xmlchildren[i].xmlname#<br>");
s[x.xmlChildren[i].xmlName] = x.xmlChildren[i].xmlText;
}
}
}
}
return s;
}
@mavelar
Copy link

mavelar commented Dec 9, 2015

Works very well!!! You just save my day!!!, actually I use the @chrishunterkiller version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment