Note that this is relatively old code, most of the logic in the view should have to go into a Presenter, to keep the view free of code. And it's specific to our application framework, so you might have to adapt it.
As I said, you can hardcode your loops when you have a known structure.
Your code will probably work as long as there is only a main menu and one level of submenu's. As soon as you introduce a second submenu level (Menu -> submenu -> sub-submenu), you code fails. You don't have that with recursion. Obviously, your css must be able to support that.
I can't judge whether or not this is an issue for you, I don't know your application. I just want to be complete in my answer, also for other people finding this thread.