Results 1 to 7 of 7

Thread: QtSql write results to XML Hierarchy for List

  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 QtSql write results to XML Hierarchy for List

    so my results for my SQL Query is

    Qt Code:
    1. 1000398| Main
    2. 1000398|1100413| Network
    3. 1000398|1100414| Plans
    4. 1000398|1100415| Support
    5. 1000402| Dine.To
    6. 1000403| TourismTo
    7. 1100399| Shopping
    8. 1100399|1100400| Store 1
    9. 1100399|1100401| Store 2
    10. 1100399|1100402| Store 3
    11. 1100399|1100403| Store 4
    12. 1100404| News
    13. 1100404|1100405| Canada
    14. 1100404|1100406| Politics
    15. 1100404|1100407| Soccer
    16. 1100408| Weather
    17. 1100409| Hotels
    18. 1100409|1100410| Holiday Inn
    19. 1100409|1100411| Hilton
    To copy to clipboard, switch view to plain text mode 

    i am writing this data using QXmlStreamWriter

    Like so

    Qt Code:
    1. QXmlStreamWriter output(&file);
    2. output.setCodec("UTF-8");
    3. output.setAutoFormatting(true);
    4. output.writeStartDocument();
    5. output.writeStartElement("Menu");
    6.  
    7. while (sqlSelFrmView.next())
    8. {
    9. output.writeStartElement("Model");
    10. output.writeTextElement("Name", sqlSelFrmView.value(1).toString());
    11. output.writeEndElement();
    12. }
    13.  
    14. output.writeEndDocument();
    To copy to clipboard, switch view to plain text mode 

    Sure this works but it write it in XML format as

    Qt Code:
    1. <Menu>
    2. <Model>
    3. <Name>Main</Name>
    4. </Model>
    5. <Model>
    6. <Name> Network</Name>
    7. </Model>
    8. </Menu>
    To copy to clipboard, switch view to plain text mode 

    now i need to do something a little different

    based off of my sql root items are level = 1 and sub items are level = 2

    how can i write the data as
    Qt Code:
    1. <Menu>
    2. <Model>
    3. <Name>Main</Name> // Level = 1
    4. <SubMenu>
    5. <Name> Network</Name> // Level = 2
    6. </SubMenu>
    7. </Model>
    8. </Menu>
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QtSql write results to XML Hierarchy for List

    this seems to be wrong: your level 1 is closed before you try to start level 2

  3. #3
    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 Re: QtSql write results to XML Hierarchy for List

    My query is as follows

    if you have any way of improving it please feel free to direct me in the correct path

    Qt Code:
    1. select concat(1000000 + a.id, '|') SORT, a.Name, a.Level
    2. from appmenu a
    3. where a.Level = 1
    4. union all
    5. select concat(1000000 + a.ID, '|', 1000000 + IFNULL(b.ID,0), '|'),concat(' ', b.Name), b.Level
    6. from appmenu a
    7. inner join appmenu a1 on a1.ParentId = a.ID
    8. inner join appmenu b on b.ID = a1.ID
    9. where a.Level = 1
    10. union all
    11. select concat(1000000 + a.ID, '|', 1000000 + IFNULL(b.ID,0), '|', 1000000 + IFNULL(c.ID,0), '|') ,concat(' ', c.Name), c.Level
    12. from appmenu a
    13. inner join appmenu a1 on a1.ParentId = a.ID
    14. inner join appmenu b on b.ID = a1.ID
    15. inner join appmenu b1 on b1.ParentId = b.ID
    16. inner join appmenu c on c.ID = b1.ID
    17. where a.Level = 1
    18. union all
    19. select concat(1000000 + a.ID, '|', 1000000 + IFNULL(b.ID,0), '|', 1000000 + IFNULL(c.ID,0), '|', 1000000 + IFNULL(d.ID,0), '|'), concat(' ', d.Name),d.Level
    20. from appmenu a
    21. inner join appmenu a1 on a1.ParentId = a.ID
    22. inner join appmenu b on b.ID = a1.ID
    23. inner join appmenu b1 on b1.ParentId = b.ID
    24. inner join appmenu c on c.ID = b1.ID
    25. inner join appmenu c1 on c1.ParentId = c.ID
    26. inner join appmenu d on d.ID = c1.ID
    27. where a.Level = 1
    28. union all
    29. select concat(1000000 + a.ID, '|', 1000000 + IFNULL(b.ID,0), '|', 1000000 + IFNULL(c.ID,0), '|', 1000000 + IFNULL(d.ID,0), '|', 1000000 + IFNULL(e.ID,0)) ,concat(' ', e.Name),e.Level
    30. from appmenu a
    31. inner join appmenu a1 on a1.ParentId = a.ID
    32. inner join appmenu b on b.ID = a1.ID
    33. inner join appmenu b1 on b1.ParentId = b.ID
    34. inner join appmenu c on c.ID = b1.ID
    35. inner join appmenu c1 on c1.ParentId = c.ID
    36. inner join appmenu d on d.ID = c1.ID
    37. inner join appmenu d1 on d1.ParentId = d.ID
    38. inner join appmenu e on e.ID = d1.ID
    39. order by SORT
    To copy to clipboard, switch view to plain text mode 

    this gives the result of

    ID# | Name | Level

    Qt Code:
    1. 1000398| Telus 1
    2. 1000398|1100413| Smartphones 2
    3. 1000398|1100414| Rate Plans 2
    4. 1000398|1100415| Coverage 2
    5. 1000398|1100416| Support 2
    6. 1000398|1100417| Promotions 2
    7. 1000402| Dine.To 1
    8. 1000403| TourismTo 1
    9. 1100399| Shopping 1
    10. 1100399|1100400| Store 1 2
    11. 1100399|1100401| Store 2 2
    12. 1100399|1100402| Store 3 2
    13. 1100399|1100403| Store 4 2
    14. 1100404| News 1
    15. 1100404|1100405| Canada 2
    16. 1100404|1100406| Politics 2
    17. 1100404|1100407| Soccer 2
    18. 1100408| Weather 1
    19. 1100409| Hotels 1
    20. 1100409|1100410| Holiday Inn 2
    21. 1100409|1100411| Hilton 2
    To copy to clipboard, switch view to plain text mode 
    Last edited by prophet0; 13th April 2012 at 11:48.

  4. #4
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QtSql write results to XML Hierarchy for List

    MMh.. Maybe I incorrectly understood what you wanted to achieve, because your SubMenu node is shifted like a child of Name node.

    Qt Code:
    1. QXmlStreamWriter output(&f);
    2.  
    3. output.setCodec("UTF-8");
    4. output.setAutoFormatting(true);
    5. output.writeStartDocument();
    6. output.writeStartElement("Menu");
    7.  
    8. output.writeStartElement("Model");
    9. output.writeTextElement("Name", "Main");
    10.  
    11. output.writeStartElement("SubMenu");
    12. output.writeTextElement("Name", "Network");
    13. output.writeEndElement();
    14.  
    15. output.writeEndElement(); // model
    16.  
    17. output.writeEndElement(); // menu
    18.  
    19. output.writeEndDocument();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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 Re: QtSql write results to XML Hierarchy for List

    See im almost there but still wont work for me could anyone see my mistake

    Qt Code:
    1. //XML stuff below
    2.  
    3. QFile iconfile("/TouchTaxiMedia/iconList.xml");
    4. if(!iconfile.open(QIODevice::WriteOnly | QIODevice::Text))
    5. {
    6. qDebug() << "Failed to open file for write";
    7. }
    8.  
    9. QXmlStreamWriter output2(&iconfile);
    10. output2.setCodec("UTF-8");
    11. output2.setAutoFormatting(true);
    12. output2.writeStartDocument();
    13. output2.writeStartElement("Menu");
    14.  
    15. while (sqlSelFrmView.next())
    16. {
    17. if(sqlSelFrmView.value(2) == "1")
    18. {
    19. output2.writeStartElement("Model");
    20. output2.writeTextElement("Name", sqlSelFrmView.value(1).toString());
    21. if (!(sqlSelFrmView.value(3) == ""))
    22. {
    23. output2.writeTextElement("Source", sqlSelFrmView.value(3).toString());
    24. }
    25. if (!(sqlSelFrmView.value(4) == ""))
    26. {
    27. output2.writeTextElement("Picture", sqlSelFrmView.value(4).toString());
    28. }
    29. if(sqlSelFrmView.value(2) == "2")
    30. {
    31. output2.writeStartElement("SubMenu");
    32. output2.writeTextElement("Name", sqlSelFrmView.value(1).toString());
    33. if (!(sqlSelFrmView.value(3) == ""))
    34. {
    35. output2.writeTextElement("Source", sqlSelFrmView.value(3).toString());
    36. }
    37. if (!(sqlSelFrmView.value(4) == ""))
    38. {
    39. output2.writeTextElement("Picture", sqlSelFrmView.value(4).toString());
    40. }
    41. output2.writeEndElement();
    42. }
    43. output2.writeEndElement();
    44.  
    45. }
    46. }
    47.  
    48. output2.writeEndDocument();
    49. //Done
    To copy to clipboard, switch view to plain text mode 

    its not adding the submenu in there at all

  6. #6
    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 Re: QtSql write results to XML Hierarchy for List

    i put line 19 to line 14 and this fixes the submenu but now i cant model to close the secound time again cause its at line 14 if there a way i can get <model> to open imput data open <submenu> then input data then close submenu and close model then do it again untill end the code i gave above works only for the first record then it does not for anything else then closes to <menu>

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QtSql write results to XML Hierarchy for List

    Quote Originally Posted by prophet0 View Post
    My query is as follows
    ...
    if you have any way of improving it please feel free to direct me in the correct path
    You may find this useful for storing a tree in an SQL database.

Similar Threads

  1. What is the type Hierarchy panel ?
    By tonnot in forum Newbie
    Replies: 2
    Last Post: 15th July 2011, 16:40
  2. object hierarchy
    By Midek in forum Newbie
    Replies: 4
    Last Post: 10th May 2010, 11:28
  3. QtSql , how to list all databases on the Mysql-Server
    By luoihoc in forum Qt Programming
    Replies: 1
    Last Post: 7th July 2009, 21:52
  4. Problem with my object hierarchy
    By rage in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 09:18
  5. QListView and changing the hierarchy
    By sgroarke in forum Newbie
    Replies: 3
    Last Post: 13th June 2007, 07:51

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
  •  
Qt is a trademark of The Qt Company.