Results 1 to 3 of 3

Thread: [SAX] program loop while reading XML file

  1. #1
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [SAX] program loop while reading XML file

    hi,

    I'm trying to parse XML file with SAX, but my program started looping. Here is my function:
    Qt Code:
    1. void JTemplate::readStyles(QXmlStreamReader &xml, QString parentId, QString id, JList<JCss> &css)
    2. {
    3. if (xml.name() == parentId)
    4. {
    5. qDebug()<<"Parsuje: "<<parentId;
    6.  
    7. while (!(xml.name() == parentId && xml.tokenType() == QXmlStreamReader::EndElement))
    8. {
    9. xml.readNext();
    10.  
    11. JCss _result;
    12. _result.setName(xml.attributes().value("name").toString());
    13. qDebug()<<"Nazwa stylu Css to: "<<_result.getName();
    14. xml.readNext();
    15.  
    16. while (!(xml.name() == id && xml.tokenType() == QXmlStreamReader::EndElement))
    17. {
    18. qDebug()<<"Styl: " << xml.name().toString();
    19. _result.setStyle(toCssStyle(xml.name().toString()), xml.readElementText());
    20. }
    21.  
    22. css.add(_result);
    23.  
    24. xml.readNext();
    25. continue;
    26. }
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    This function was written to parese parts of XML file like:
    Qt Code:
    1. <visited>
    2. <link name="bink1_visited">
    3. <FontColor>red</FontColor>
    4. <FontColor2>green</FontColor2>
    5. </link>
    6. </visited>
    To copy to clipboard, switch view to plain text mode 
    When I use this function for example
    Qt Code:
    1. readStyles(_xml, "visited", "link", CssAnchorVisited);
    To copy to clipboard, switch view to plain text mode 
    the loop
    Qt Code:
    1. while (!(xml.name() == id && xml.tokenType() == QXmlStreamReader::EndElement))
    To copy to clipboard, switch view to plain text mode 
    is working indefinitely. Why? What do I do wrong?

    And the second thing, I cannot read arguments (in my code "name") in line:
    Qt Code:
    1. _result.setName(xml.attributes().value("name").toString());
    To copy to clipboard, switch view to plain text mode 

    ps. Sory for my English :-) I'm still learning

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [SAX] program loop while reading XML file

    As for the while statement, you have two readNext in the loop - perhaps you skip the end tag that you are looking for.

  3. #3
    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: [SAX] program loop while reading XML file

    Quote Originally Posted by kabanek View Post
    the loop
    Qt Code:
    1. while (!(xml.name() == id && xml.tokenType() == QXmlStreamReader::EndElement))
    To copy to clipboard, switch view to plain text mode 
    is working indefinitely. Why? What do I do wrong?
    What is this while() loop supposed to do?

    And the second thing, I cannot read arguments (in my code "name") in line:
    Qt Code:
    1. _result.setName(xml.attributes().value("name").toString());
    To copy to clipboard, switch view to plain text mode 
    Attributes are reported in StartElement and not EndElement.

    I'm not sure what you want to achieve but I think you are overcomplicating things, try this:
    Qt Code:
    1. JList<JCss> JTemplate::readStyles(QXmlStreamReader &xml, QString parentId, QString id )
    2. {
    3. JList<JCss> css;
    4. Q_ASSERT(xml.name() == parentId);
    5. while(xml.readNextStartElement() {
    6. if(xml.name() == parentId) {
    7. while(xml.readNextStartElement()){
    8. if(xml.name() == id){
    9. JCss _result;
    10. _result.setName(xml.attributes().value("name").toString());
    11. qDebug() << "Styl:" << xml.name();
    12. _result.setStyle(...); // I don't think readElementText() will work here without using a CDATA section
    13. css.add(_result);
    14. } else xml.skipCurrentElement(); // skip unknown tags
    15. }
    16. xml.skipCurrentElement(); // read until end of parentId tag
    17. } else xml.skipCurrentElement(); // skip unknown tags
    18. }
    19. return css;
    20. }
    To copy to clipboard, switch view to plain text mode 
    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. Phonon - how to loop a video file
    By Mrdata in forum Newbie
    Replies: 6
    Last Post: 24th July 2015, 18:31
  2. Reading XML file using DOM
    By rk0747 in forum Qt Programming
    Replies: 6
    Last Post: 4th February 2010, 22:09
  3. reading xml file
    By dreamer in forum Qt Programming
    Replies: 3
    Last Post: 5th May 2008, 02:01
  4. Reading from sockets in a multithreaded program
    By KoosKoets in forum Qt Programming
    Replies: 9
    Last Post: 4th April 2007, 20:43
  5. Reading from TCP Socket crashes program
    By OnionRingOfDoom in forum Qt Programming
    Replies: 26
    Last Post: 27th January 2006, 19:32

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.