i cant seem to get my XML to write properly in a while/if loop

please see my code as follows

Qt Code:
  1. QXmlStreamWriter output2(&iconfile);
  2. output2.setAutoFormatting(true);
  3. output2.writeStartDocument();
  4. output2.writeStartElement("Menu");
  5. while (sqlSelFrmView.next())
  6. {
  7. QString levelString = sqlSelFrmView.value(2).toString();
  8. output2.writeStartElement("Parent");
  9. if(levelString == "1")
  10. {
  11.  
  12. output2.writeTextElement("Root", sqlSelFrmView.value(1).toString());
  13. }
  14.  
  15. if(levelString != "1")
  16. {
  17. output2.writeStartElement("Child");
  18. output2.writeTextElement("Name", sqlSelFrmView.value(1).toString());
  19. }
  20. output2.writeEndElement();
  21. }
  22. output2.writeEndElement();
  23. output2.writeEndElement();
  24. output2.writeEndDocument();
To copy to clipboard, switch view to plain text mode 

but the result of my xml document is not as expected

Qt Code:
  1. <Menu>
  2. <Parent>
  3. <Root>Telus</Root>
  4. </Parent>
  5. <Parent>
  6. <Child>
  7. <Name>Smartphones</Name>
  8. </Child>
  9. <Parent>
  10. <Child>
  11. <Name>Rate Plans</Name>
  12. </Child>
  13. <Parent>
  14. <Child>
  15. <Name>Coverage</Name>
  16. </Child>
  17. <Parent>
  18. <Child>
  19. <Name>Support</Name>
  20. </Child>
  21. <Parent>
  22. <Child>
  23. <Name>Promotions</Name>
  24. </Child>
  25. <Parent>
  26. <Root>Dine.To</Root>
  27. </Parent>
  28. <Parent>
  29. <Root>TourismTo</Root>
  30. </Parent>
  31. </Parent>
  32. </Parent>
  33. </Parent>
  34. </Menu>
To copy to clipboard, switch view to plain text mode 

my desired result is

Qt Code:
  1. <Menu>
  2. <Parent>
  3. <Root>Telus</Root>
  4. <Child>
  5. <Name>Smartphones</Name>
  6. </Child>
  7. <Child>
  8. <Name>Rate Plans</Name>
  9. </Child>
  10. </Parent>
  11. <Parent>
  12. <Root>Telus</Root>
  13. <Child>
  14. <Name>Smartphones</Name>
  15. </Child>
  16. <Child>
  17. <Name>Rate Plans</Name>
  18. </Child>
  19. </Parent>
  20. <Parent>
  21. <Root>Dine.To</Root>
  22. </Parent>
  23. <Menu>
To copy to clipboard, switch view to plain text mode 

any pointers would be much appreciated