One Star

Recursive Nested XML Loop with repeating elements

Hi...
I have a nested loop and trying to extract the data in csv format.
For eg:
The input XML is as shown below:
<cfxml variable="xmlTasks">
<tasks>
<task>
<name>Breakfast</name>
<task>
<name>Cook Eggs</name>
<task>
<name>Crack Eggs</name>
<task>
<name>Crack Egg 1</name>
</task>
<task>
<name>Crack Egg 2</name>
</task>
<task>
<name>Crack Egg 3</name>
</task>
<task>
<name>Crack Egg 4</name>
</task>
</task>
<task>
<name>Beat Eggs</name>
<task>
<name>Beat Clockwise</name>
<task>
<name>Beat</name>
</task>
<task>
<name>Beat</name>
</task>
<task>
<name>Beat</name>
</task>
</task>
<task>
<name>Beat Counter Clockwise</name>
<task>
<name>Beat</name>
</task>
<task>
<name>Beat</name>
</task>
<task>
<name>Beat</name>
</task>
</task>
</task>
<task>
<name>Put Eggs In Pan</name>
</task>
<task>
<name>Put Eggs on Plate</name>
</task>
</task>
<task>
<name>Eat Eggs</name>
<task>
<name>Put In Mouth</name>
<task>
<name>Put On Fork</name>
</task>
<task>
<name>Move For To Mouth</name>
</task>
</task>
<task>
<name>Chew Eggs</name>
</task>
<task>
<name>Swallow</name>
</task>
</task>
</task>
</tasks>
</cfxml>
The output should be
Task; SubTask
Breakfast;Cook Eggs,Eat Eggs
Cook Eggs;Crack Eggs,Beat Eggs,Put Eggs In Pan,Put Eggs on Plate
Crack Eggs;Crack Egg 1,Crack Egg 2,Crack Egg 3,Crack Egg 4
Beat Eggs;Beat Clockwise,Beat Counter Clockwise
Beat Clockwise;Beat,Beat,Beat
Beat Counter Clockwise;Beat,Beat,Beat
Eat Eggs;Put In Mouth,Chew Eggs,Swallow
Put In Mouth;Put On Fork,Move For To Mouth
So, my goal is to get the tasks and subtasks listed out. Since, the number of subtasks is unknown, I am not sure using fileinputxml and setting the xpath loop would help.
Kindly let me know if any one has faced a similar problem.
Regards
Kumar
1 REPLY

Re: Recursive Nested XML Loop with repeating elements

For detailed explanation of reading and processing XML file with multiple nested loops visit the blog post
http://vikramtakkar.blogspot.in/2013/02/read-xml-having-multiple-nested-loops.html