PDA

View Full Version : Looking for sample code that uses multiple delegates to parse xml on iphone




unagimiyagi
Aug 5, 2010, 06:19 PM
Hi, I'm trying to parse this menu structure and put it into a drill-down tableview. I think that I need multiple delegates, but if someone has an actual full-fledge sample, not just snippets of code, this would help me out.

The reason that this is hard is b/c menuitems can have menuitems which in turn can have its own menuitems. The tags keep repeating and the sequential parser is proving very difficult for me.





1. <testxml>
2. <Menu>
3. <MenuItem>
4. <menuName>Menu 1</menuName>
5. <MenuChildren>
6. <MenuItem>
7. <menuName>Sub-Menu 1</menuName>
8. </MenuItem>
9. <MenuItem>
10. <menuName>Sub-Menu 1a</menuName>
11. <MenuChildren>
12. <MenuItem>
13. <menuName>Sub-Sub-Menu 1aa</menuName>
14. </MenuItem>
15. </MenuChildren>
16. </MenuItem>
17. </MenuChildren>
18. </MenuItem>
19. <MenuItem>
20. <menuName>Menu 2</menuName>
21. </MenuItem>
22. <MenuItem>
23. <menuName>Menu 3</menuName>
24. </MenuItem>
25. <MenuItem>
26. <menuName>Menu 4</menuName>
27. </MenuItem>
28. </Menu>
29. </testxml>



kainjow
Aug 5, 2010, 09:49 PM
You could try using a stack of menu item objects. When you start a <MenuItem> element, create a corresponding object and push it onto the stack. Before pushing it onto the stack, you would add it to the top item of the stack. When you come to a </MenuItem> ending element, pop it off the stack. That way the top item on the stack is the current menu item being read. Before you start reading the XML you could pop onto the stack a "root" menu item object that holds all the top level items.