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;
}
@aiKrice
Copy link

aiKrice commented Jan 22, 2013

lets take a look on this:

< cffunction name="xmlToJson" returntype="any" hint="convert xml to JSON">

        <cfscript>
        var s = {};
        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");
                    }
                 }
            }
        }}

        </cfscript>
        <cfreturn s>
    </cffunction>

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
          <urn1:MemberOf>crid://thomson.net/service/5.PDSQOS</urn1:MemberOf>
          <urn1:MemberOf>crid://thomson.net/service/5.PDSTEST</urn1:MemberOf>
          <urn1:MemberOf>crid://thomson.net/service/5.PDSTEST_ADSL</urn1:MemberOf>
          <urn1:EpgId>5</urn1:EpgId>
        </urn1:ServiceInformation>
        </urn1:ServiceInformationTable></urn1:ProgramDescription></urn1:TVAMain></urn:getServicesByServicePackageResult></soapenv:Body></soapenv:Envelope>

<cfset r = createObject("component", "com.Xml").xmlToJson(xml)>

@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