Probably your easiest solution is to use a JQuery plugin like this one https://github.com/tommcfarlin/Collapsible-Menus. It's easy to use, and doesn't require much in the way of additional markup.
However, you're trying to do this with Bootstrap, so I'll try to help you find a solution using that. The first thing to notice is some errors in your code;
<ul id="accordiongroup_2" class"collapse in">
should be
<ul id="accordiongroup_2" class="collapse in">
That may be preventing some of the crucial styles from working. Essentially, to collapse a sub-menu, you just need to give the 'ul' a class of "collapsed". It's then a matter of using logic to work out which is your active menu item, and not adding the collapsed class to that.
Your problem will arise trying to work out the parent 'li' so you don't collapse them either. One way to do this on the server side might be to create a small function that tests if an 'li' element has a child 'ul' with a child 'li' that is active. If so, then set that 'li' to be active too. You'll need to get the data as a nested array first, and you'll need to run it though the number of times as your maximum nest depth.