CFLib.org – Common Function Library Project

flattenXmlToStruct(xmlObject [, delimiter] [, prefix] [, stResult] [, addPrefix])

Last updated February 24, 2011

Version: 1 | Requires: ColdFusion 8 | Library: DateLib

 
Rated 2 time(s). Average Rating: 4.0

Description:
Recursively traverses through an XML object with nested nodes and builds a flattened structure object with a concatenated-key from the nested nodes. This is useful for capturing data values from a PDF Form submitted in an XML format. (http://ardenharrell.blogspot.com/2011/02/coldfusion-flatten-xml-object-to-struct.html)

Return Values:
Returns a struct.

Example:

view plain print about
<cfxml variable="xmlText">
<?xml version="1.0" encoding="UTF-8"?>
<fields xmlns:xfdf="http://ns.adobe.com/xfdf-transition/">
  <first_name>Stanley</first_name>
  <last_name>Silvers</last_name>
  <department>Marketing</department>
  <email>Stanley.Silvers@funkyDomain.com</email>
  <favorite_info>
    <favorite_food>Muffins</favorite_food>
    <favorite_shoe>Crocs</favorite_shoe>
    <favorite_place>Tim Horton's</favorite_place>
  </favorite_info>
</fields>
</cfxml>

<cfset stFlattened = flattenXmlToStruct(xmlText.fields)>

<cfset stFlattened = flattenXmlToStruct(xmlObject: xmlText.fields, delimiter: "@")>

Parameters:

Name Description Required
xmlObject XML object to flatten. Yes
delimiter Delimits keys. Defaults to . No
prefix Prefixes keys. Defaults to nothing. No
stResult Result struct. Allows you to pass in a structure by reference. No
addPrefix Boolean that determines if the prefix attribute is used. Defaults to tru. No

Full UDF Source:

view plain print about
<!---
 Converts an XML Object into a single flattened struct.
 
 @param xmlObject      XML object to flatten. (Required)
 @param delimiter      Delimits keys. Defaults to . (Optional)
 @param prefix      Prefixes keys. Defaults to nothing. (Optional)
 @param stResult      Result struct. Allows you to pass in a structure by reference. (Optional)
 @param addPrefix      Boolean that determines if the prefix attribute is used. Defaults to tru. (Optional)
 @return Returns a struct. 
 @author Arden Harrell (ArdMan_Da_Codehead@YAHOO.com) 
 @version 1, February 24, 2011 
--->

<cffunction name="flattenXmlToStruct" access="public" output="false" returntype="struct">
    <cfargument name="xmlObject" required="true" type="xml" />
    <cfargument name="delimiter" required="false" type="string" default="." />
    <cfargument name="prefix"      required="false" type="string" default="" />
    <cfargument name="stResult" required="false" type="struct" />
    <cfargument name="addPrefix" required="false" type="boolean" default="true" />
    
    <cfset var sKey = '' />
    <cfset var currentKey = "">
    <cfset var arrIndx = "">
    
    <cfif NOT isDefined("arguments.stResult")>
      <cfset arguments.stResult = structNew()>
    </cfif>
    
    <cfloop from="1" to="#ArrayLen(arguments.xmlObject.XmlChildren)#" index="arrIndx">
       
       <cfset sKey = arguments.xmlObject.XmlChildren[arrIndx].XmlName>
       
       <cfif ArrayLen(arguments.xmlObject.XmlChildren[arrIndx].XmlChildren) EQ 0>
            <cfif arguments.addPrefix and len(arguments.prefix)>
                <cfset arguments.stResult["#arguments.prefix##arguments.delimiter##sKey#"] = arguments.xmlObject.XmlChildren[arrIndx].XmlText />
            <cfelse>
                <cfset arguments.stResult[sKey] = arguments.xmlObject.XmlChildren[arrIndx].XmlText />
            </cfif>
         
       <cfelse>    <!--- Node has more children... --->

            <cfif arguments.prefix EQ "">
              <cfset currentKey = sKey>
            <cfelse>
              <cfset currentKey = "#arguments.prefix##arguments.delimiter##sKey#">
            </cfif>
            <cfset flattenXmlToStruct(arguments.xmlObject.XmlChildren[arrIndx], arguments.delimiter, currentKey, arguments.stResult) />    
       
       </cfif>
    </cfloop>
    
    <cfreturn arguments.stResult />
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson