Results 1 to 2 of 2

Thread: QXmlStreamWriter and while/if loop

  1. #1
    Join Date
    Oct 2011
    Location
    Toronto Canada
    Posts
    97
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QXmlStreamWriter and while/if loop

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QXmlStreamWriter and while/if loop

    Your code structure is "flat" so you get flat output. You need to make your code hierarchical -- if you expect to read a child, don't close the parent tag until you know there is no child. If you can't handle that directly then first use your flat code to create a regular tree of objects in memory and then traverse this tree to write it to xml, it should be much simpler then because you'll be able add children to already created elements.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 6th August 2011, 02:40
  2. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 17:31
  3. Multiline comment with QXmlStreamWriter
    By QtWannabe in forum Qt Programming
    Replies: 2
    Last Post: 30th November 2010, 14:05
  4. QXmlStreamWriter UTF-16 Problem
    By 27Loco in forum Qt Programming
    Replies: 3
    Last Post: 7th July 2010, 14:04
  5. Readable Xml with QXmlStreamWriter
    By jano_alex_es in forum Newbie
    Replies: 8
    Last Post: 26th August 2009, 12:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.