Created
January 20, 2013 18:20
-
-
Save cfjedimaster/4580449 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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
lets take a look on this:
< cffunction name="xmlToJson" returntype="any" hint="convert xml to JSON">
and my test xml:
soapenv:Body urn1:Pagination urn1:numOfItems10/urn1:numOfItems urn1:startIndex10/urn1:startIndex urn1:endIndex15/urn1:endIndex urn1:numTotalOfItems305/urn1:numTotalOfItems /urn1:Pagination /urn1:TablePagination /urn1:TableSortingInformation urn1:ProgramDescription urn1:ServiceInformationTable urn1:MemberOfcrid://thomson.net/service/5.PDSQOS/urn1:MemberOf urn1:MemberOfcrid://thomson.net/service/5.PDSTEST/urn1:MemberOf urn1:MemberOfcrid://thomson.net/service/5.PDSTEST_ADSL/urn1:MemberOf urn1:EpgId5/urn1:EpgId /urn1:ServiceInformation<cfset r = createObject("component", "com.Xml").xmlToJson(xml)>