I think this behavior is caused by the last line of your loop (cntLn--;).
Assume, the list has one child, so you start your processing with cntln=0 and length=1 which means: execute the loop. Then you remove the node since the condition is evaluated to true and you decrease the counter to -1. After that your for-loop increases cntln again to 0 and you start again but now the node list of the Lines-element is empty!
Simply omit the last line and try again ;)
Nevertheless, I think this is a critical processing since you cannot foresee side-effects when removing nodes while looping over the node list. Maybe it would be better to store the index of each node to be removed in this loop and remove them later in another loop.