Skip to content

Instantly share code, notes, and snippets.

@redherring917
Last active December 31, 2015 03:48
Hi! Using the conversion function below (file 4) against the sample XML in file 1 below results in the JSON formatted output seen in file 2 below. But what I really need is JSON formatted as seen in file 3 below. I found that I needed to specifically include the "element" notation in creating my XML in order to have the structure of the file pro…
<productCategoriesList>
<element>
<id>1</id>
<name>Long Sleeve Tees</name>
<products>
<element>
<id>11</id>
<name>Cool Tee 1</name>
<thumbUrl>products/cool-t-shirt/thumbs/t-shirt-yellow.png</thumbUrl>
</element>
<element>
<id>11-t</id>
<name>Cool Tee 1 - template</name>
<thumbUrl>products/cool-t-shirt/thumbs/t-shirt-black.png</thumbUrl>
</element>
</products>
</element>
<element>
<id>2</id>
<name>V-Neck Tees</name>
<products>
<element>
<id>21</id>
<name>Cool Tee 2</name>
<thumbUrl>products/cool-t-shirt/thumbs/t-shirt-blue.png</thumbUrl>
</element>
<element>
<id>22</id>
<name>Cool Tee 3</name>
<thumbUrl>products/cool-t-shirt/thumbs/t-shirt-red.png</thumbUrl>
</element>
</products>
</element>
</productCategoriesList>
{"productCategoriesList":{"element":[{"name":"Long Sleeve Tees","products":{"element":[{"name":"Cool Tee 1","id":11,"thumbUrl":"products\/cool-t-shirt\/thumbs\/t-shirt-yellow.png"},{"name":"Cool Tee 1 - template","id":"11-t","thumbUrl":"products\/cool-t-shirt\/thumbs\/t-shirt-black.png"}]},"id":1},{"name":"V-Neck Tees","products":{"element":[{"name":"Cool Tee 2","id":21,"thumbUrl":"products\/cool-t-shirt\/thumbs\/t-shirt-blue.png"},{"name":"Cool Tee 3","id":22,"thumbUrl":"products\/cool-t-shirt\/thumbs\/t-shirt-red.png"}]},"id":2}]}}
{"productCategoriesList":[{"name":"Long Sleeve Tees","products":[{"name":"Cool Tee 1","id":11,"thumbUrl":"products\/cool-t-shirt\/thumbs\/t-shirt-yellow.png"},{"name":"Cool Tee 1 - template","id":"11-t","thumbUrl":"products\/cool-t-shirt\/thumbs\/t-shirt-black.png"}],"id":1},{"name":"V-Neck Tees","products":[{"name":"Cool Tee 2","id":21,"thumbUrl":"products\/cool-t-shirt\/thumbs\/t-shirt-blue.png"},{"name":"Cool Tee 3","id":22,"thumbUrl":"products\/cool-t-shirt\/thumbs\/t-shirt-red.png"}],"id":2}]}
<cfscript>
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;
}
s = xmlToStruct(xmlData);
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment