[FMX] Recursive Parser

In my editor i’ve got a dragable menu - where menu items can be dragged up and down and can also be placed as sub menu items. I want to save the structure of this menu in xml. Remember the menu items can contain menu items that contain menu items and so on - forever.

How do i go about creating the xml that reflects this structure - as far as i understand i have to make some kind of recursive function that places each menu item node in the right place in the xml-object. But I can’t get my head around it.

The part that i really can’t figure out is how to place the xml-node in the correct place in the xml-tree and do it dynamically.

Say i have a menu item with a menu item with a menu item. When i have parsed the third level menu item into an xml-node how do i place that node as a child of the second level node?

And even worse how would i go about placing a new second level menu item after the third level menu item?

Any helpful code-snippets, code-outlines or tutorials would be very greatly appreciated.

Thank you all very much for the anticipated solution to my problems.