Results 1 to 9 of 9

Thread: Parsing XML

  1. #1
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 7 Times in 6 Posts

    Default Parsing XML

    Hello!

    I'm trying to parse xml document:
    Qt Code:
    1. <xml_api_reply version="1">
    2. <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
    3.  
    4. <forecast_information>
    5. <city data="Gdansk, Pomerania"/>
    6. <postal_code data="Gdansk,Poland"/>
    7. <latitude_e6 data=""/>
    8. <longitude_e6 data=""/>
    9. <forecast_date data="2010-12-09"/>
    10. <current_date_time data="2010-12-09 23:00:00 +0000"/>
    11. <unit_system data="SI"/>
    12. </forecast_information>
    13.  
    14. <current_conditions>
    15. <condition data="Zachmurzenie"/>
    16. <temp_f data="30"/>
    17. <temp_c data="-1"/>
    18. <humidity data="Wilgotność: 86%"/>
    19. <icon data="/ig/images/weather/cloudy.gif"/>
    20. <wind_condition data="Wiatr: płn.-zach. z szybkością 26 km/h"/>
    21. </current_conditions>
    22. [...]
    To copy to clipboard, switch view to plain text mode 

    My code:
    Qt Code:
    1. ByteArray newData = NetRepl->read(2048);
    2.  
    3. out << newData << endl;
    4.  
    5. QXmlStreamReader xmlStream(newData);
    6. while(!xmlStream.atEnd())
    7. {
    8. xmlStream.readNext();
    9. if(xmlStream.isStartElement())
    10. {
    11. QString sec(xmlStream.name().toString());
    12. out << sec << endl;
    13.  
    14. while (!xmlStream.isEndElement())
    15. {
    16. xmlStream.readNext();
    17. QString sec(xmlStream.name().toString());
    18. out << "***" << sec << endl;
    19.  
    20. QXmlStreamAttributes attrs = xmlStream.attributes();
    21. out << attrs.value("data").toString() << endl;
    22. }
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    And it sometimes returns "data" correctly, and sometimes it's empty:

    Qt Code:
    1. xml_api_reply
    2. ***weather
    3.  
    4. ***forecast_information
    5.  
    6. ***city
    7. Gdansk, Pomerania //OK
    8. ***city
    9.  
    10. postal_code
    11. ***postal_code
    12.  
    13. latitude_e6
    14. ***latitude_e6
    15.  
    16. longitude_e6
    17. ***longitude_e6
    18.  
    19. forecast_date
    20. ***forecast_date
    21.  
    22. current_date_time
    23. ***current_date_time
    24.  
    25. unit_system
    26. ***unit_system
    27.  
    28. current_conditions
    29. ***condition
    30. Zachmurzenie //OK
    31. ***condition
    32.  
    33. temp_f
    34. ***temp_f
    35.  
    36. temp_c
    37. ***temp_c
    38.  
    39. humidity
    40. ***humidity
    41.  
    42. icon
    43. ***icon
    44.  
    45. wind_condition
    46. ***wind_condition
    To copy to clipboard, switch view to plain text mode 

    How should I do It correctly?

    thanks in advance
    best regards
    Tomasz

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

    Default Re: Parsing XML

    What I usually do is that I break such code into several methods where each single method is responsible for parsing one tag (or level) of xml. Then it's easier to spot errors. You are not checking errors anywhere here and you should be since the data comes from the network. Try finding a pattern in failures of your parser.
    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.


  3. The following user says thank you to wysota for this useful post:

    Tomasz (10th December 2010)

  4. #3
    Join Date
    Dec 2010
    Location
    BEIJING CHINA
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 2 Times in 2 Posts

    Default Re: Parsing XML

    the following code can help you.
    you can get every value of "data".
    Qt Code:
    1. QXmlStreamAttributes attrs = xmlStream.attributes();
    2. if (attrs.hasAttribute("data")) {
    3. xmlStream.readNextStartElement();
    4. } else {
    5. xmlStream.readNext();
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by xiaokui; 10th December 2010 at 05:03.

  5. The following user says thank you to xiaokui for this useful post:

    Tomasz (10th December 2010)

  6. #4
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 7 Times in 6 Posts

    Default Re: Parsing XML

    I've broke code in couple methods and, and used attrs.hasAttribute() and now it works fine. One more question connected with thread. I'm reading XML document with:

    header file:
    Qt Code:
    1. private:
    2. QNetworkAccessManager NetAccMan;
    3. QNetworkReply *NetRepl;
    To copy to clipboard, switch view to plain text mode 

    main file:
    Qt Code:
    1. void MainWindow::getXML()
    2. {
    3. QNetworkRequest request(QUrl("http://www.google.com/ig/api?weather=Gdansk,Poland&hl=pl"));
    4. NetRepl = NetAccMan.get(request);
    5. connect(NetRepl, SIGNAL(readyRead()), this, SLOT(parseXML()));
    6. }
    7.  
    8. void MainWindow::parseXML()
    9. {
    10. }
    To copy to clipboard, switch view to plain text mode 

    But I'm wondering if I need to clear NetRepl or something after each read/parse, because as I assume QNetworkAccessManager creates QNetworkReply, and returns pointer to it each time I call getXML(); Maybe I should use same clearing function of QNetworkReply?

    thanks in advance
    best regards
    Tomasz
    Last edited by Tomasz; 10th December 2010 at 13:44.

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

    Default Re: Parsing XML

    QNetworkAccessManager docs should specify who is the owner of the network reply object.
    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.


  8. #6
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 7 Times in 6 Posts

    Default Re: Parsing XML

    Quote Originally Posted by wysota View Post
    QNetworkAccessManager docs should specify who is the owner of the network reply object.
    It says about QNetworkReply * QNetworkAccessManager::get ( const QNetworkRequest & request ): "It returns a new QNetworkReply object opened for reading which emits its QIODevice::readyRead() signal whenever new data arrives.". So is QNetworkAccessManager owner of networ reply object? And takes care about this object after downloading data?

    thanks in advance
    best regards
    Tomasz

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

    Default Re: Parsing XML

    I think this is more important:

    Quote Originally Posted by QNetworkAccessManager docs
    Note: After the request has finished, it is the responsibility of the user to delete the QNetworkReply object at an appropriate time. Do not directly delete it inside the slot connected to finished(). You can use the deleteLater() function.
    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.


  10. The following user says thank you to wysota for this useful post:

    Tomasz (11th December 2010)

  11. #8
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    70
    Thanked 7 Times in 6 Posts

    Default Re: Parsing XML

    I was searching in function description. I didn't noticed that part in detailed description of class - sorry. So It would be all right if I put deleteLater() in my getXML() function at the end of it? Or Is it necessary to put it in slot connected to 'finished' signal?

    thanks in advance
    best regards
    Tomasz

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

    Default Re: Parsing XML

    It's your responsibility to delete the object so you can do it whenever you want.
    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. DTD Parsing in qt4?
    By darshan.hardas in forum Qt Programming
    Replies: 0
    Last Post: 1st July 2009, 10:39
  2. XML parsing
    By systemz89 in forum Qt Programming
    Replies: 4
    Last Post: 29th December 2007, 18:31
  3. Parsing Files
    By JayJay in forum Qt Programming
    Replies: 13
    Last Post: 20th August 2007, 19:01
  4. XML Parsing in Qt 3.3
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2007, 18:54
  5. Xml parsing
    By probine in forum Qt Programming
    Replies: 4
    Last Post: 15th December 2006, 11:28

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.