Created
March 28, 2020 23:45
-
-
Save robertz/61c44c771eba67061816ac4e9394c5a9 to your computer and use it in GitHub Desktop.
XML Parser
This file contains hidden or 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
<cffunction name="xmlToStruct" access="public" returntype="struct" output="false"> | |
<cfargument name="xmlNode" type="string" required="true" /> | |
<cfscript> | |
var res = {}; | |
var xmlDoc = xmlSearch(xmlParse(xmlNode), "/node()")[1]; | |
for(var i = 1; i <= xmlDoc.xmlChildren.len(); i++){ | |
var n = replace(xmlDoc.XmlChildren[i].XmlName, xmlDoc.XmlChildren[i].XmlNsPrefix & ":", ""); | |
if(res.keyExists(n)){ | |
if(!isArray(res[n])){ | |
tmp = res[n]; | |
res[n] = []; | |
res[n][1] = tmp; | |
} | |
if(xmlDoc.xmlChildren[i].xmlChildren.len()){ | |
res[n][res[n].len() + 1] = xmlToStruct(xmlDoc.XmlChildren[i]); | |
} | |
else{ | |
res[n][res[n].len() + 1] = xmlDoc.XmlChildren[i].XmlText; | |
} | |
} | |
else{ // key does not exist | |
if(xmlDoc.XmlChildren[i].XmlChildren.len()){ | |
res[n] = xmlToStruct(xmlDoc.XmlChildren[i]); | |
} | |
else{ | |
if(isStruct(xmlDoc.XmlChildren[i].XmlAttributes) && structCount(xmlDoc.XmlChildren[i].XmlAttributes)){ | |
res[n] = xmlDoc.XmlChildren[i].XmlText; | |
var attrib_list = structKeylist(xmlDoc.XmlChildren[i].XmlAttributes); | |
for(var j = 1; j <= listLen(attrib_list); j++){ | |
if(listGetAt(attrib_list, j) contains "xmlns:"){ | |
structdelete(xmlDoc.XmlChildren[i].XmlAttributes, listGetAt(attrib_list, j)); | |
} | |
} | |
if(structCount(xmlDoc.XmlChildren[i].XmlAttributes)){ | |
res[n & '_attributes'] = xmlDoc.XmlChildren[i].XmlAttributes; | |
} | |
} | |
else{ | |
res[n] = xmlDoc.XmlChildren[i].XmlText; | |
} | |
} | |
} | |
} | |
return res; | |
</cfscript> | |
</cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment