What I'm trying to do is read an XML file, then output a taxonomy or categorization path. The taxonomy is written in the XML file as a bunch of nested nodes, so it's pretty obvious that this is going to require some kind of recursion.
The XML file looks kind of like this:
<cfxml variable="xmlBulletin">
<bulletin>
<contents>
...
</contents>
<taxonomy>
<category1>
<subcategory1>
<subcategory2>
true
</subcategory2>
<subcategory3>
<subcategory4>
true
</subcategory4>
</subcategory3>
</subcategory1>
</category1>
</taxonomy>
</bulletin>
</cfxml>
This basically means that my outputted taxonomy paths should be:
/taxonomy/category1/subcategory1/subcategory2
/taxonomy/category1/subcategory1/subcategory3/subcategory4
Now, I've attempted to write a function to output those paths in the above format, but I need a little help. Currently, my function only works for one path. I've tried to modify it to handle the whole tree, but I run into problems when siblings are introduced. For example, having subcategory2 and subcategory3 both part of subcategory1...
<cfset taxPathVar = ""> <!--- Stored outside of the function's scope --->
<cffunction name="getFullTaxonomyPath" returntype="string">
<cfargument name="inXML" type="xml" required="yes">
<cfargument name="firstRun" type="boolean" required="yes" default=true>
<cfif firstRun>
<cfset taxPathVar = "">
</cfif>
<cfset taxPathVar = taxPathVar & "/" & inXML.XmlName>
<cfif ArrayLen(inXML.xmlChildren)>
<cfset myvar = getFullTaxonomyPath(inXML=inXML.xmlChildren[1], firstRun=false)>
</cfif>
<cfreturn taxPathVar>
</cffunction>
Called with:
<cfoutput>#getFullTaxonomyPath(xmlBulletin.bulletin.taxonomy)#</cfoutput>
It currently only outputs one path:
/taxonomy/category1/subcategory1/subcategory2
I'm just having trouble wrapping my head around how to let my function keep track of where it's been in the tree...
Any kind of suggestion would be helpful... Can this even be done with one recursive function? Thanks.
Full Source
This post has been edited by SleepingInChapel: 31 March 2011 - 02:47 PM

New Topic/Question
Reply




MultiQuote





|