Results 1 to 3 of 3

Thread: Parsing RSS 2.0 using QXmlStreamReader

  1. #1
    Join Date
    Aug 2011
    Posts
    38
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Parsing RSS 2.0 using QXmlStreamReader

    Hi

    i''m currently parse RSS 2.0 feed using QXmlStreamReader-class. All does well but I can't catch enclosure-element. I.e

    Qt Code:
    1. <enclosure url="http://www.w3schools.com/media/3d.wmv" length="78645" type="video/wmv" />
    To copy to clipboard, switch view to plain text mode 

    This is my xml parsing routine:
    Qt Code:
    1. while (!m_xml.atEnd()) {
    2. m_xml.readNext();
    3. if (m_xml.isStartElement()) {
    4. m_currentTag = m_xml.name().toString();
    5. } else if (m_xml.isEndElement()) {
    6. if (m_xml.name() == "item") {
    7. if (!m_feeds.contains(feedItem)) {
    8. feeds.append(feedItem);
    9. newFeeds = true;
    10. }
    11. feedItem.m_category.clear();
    12. feedItem.m_title.clear();
    13. feedItem.m_link.clear();
    14. feedItem.m_quid.clear();
    15. feedItem.m_description.clear();
    16. }
    17. } else if (m_xml.isCharacters() && !m_xml.isWhitespace()) {
    18. if (m_currentTag == "title") {
    19. feedItem.m_title += m_xml.text().toString();
    20. }
    21. else if (m_currentTag == "link") {
    22. feedItem.m_link += m_xml.text().toString();
    23. }
    24. else if (m_currentTag == "category") {
    25. feedItem.m_category += m_xml.text().toString();
    26. }
    27. else if (m_currentTag == "pubDate") {
    28. convertPublishDate(feedItem.m_pubDate, m_xml.text().toString());
    29. }
    30. else if (m_currentTag == "description") {
    31. feedItem.m_description += m_xml.text().toString();
    32. }
    33. else if (m_currentTag == "guid") {
    34. feedItem.m_quid += m_xml.text().toString();
    35. } else if (m_currentTag == "enclosure") {
    36. qDebug () << m_xml.text().toString();
    37. }
    38. } else if (m_xml.isComment())
    39. qDebug () << m_xml.text().toString();
    To copy to clipboard, switch view to plain text mode 

    How to catch the enclosure-element? Should I change to use some xml parse class ?

    thanks,

  2. #2
    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: Parsing RSS 2.0 using QXmlStreamReader

    The empty enclosure element is both isStartElement() and isEndElement(). Your code will only process the start element portion of the logic before readNext() is called and the next element is fetched. You could look at the recursive descent approach in the QXmlStreamReader example.

  3. #3
    Join Date
    Aug 2011
    Posts
    38
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Parsing RSS 2.0 using QXmlStreamReader

    thanks to tip !

    I did little modifications to my code :
    Qt Code:
    1. while (!m_xml.atEnd()) {
    2. m_xml.readNext();
    3. if (m_xml.isStartElement()) {
    4. m_currentTag = m_xml.name().toString();
    5. qDebug () << "start " << m_currentTag;
    6. if (m_xml.name() == "enclosure" && newFeeds) {
    7. feedItem.m_enclosure.m_url = m_xml.attributes().value("url").toString();
    8. feedItem.m_enclosure.m_type = m_xml.attributes().value("type").toString();
    9. }
    10. } else if (m_xml.isEndElement()) {
    11. if (m_xml.name() == "item") {
    To copy to clipboard, switch view to plain text mode 

    This solved my problem. This may be not efficient but now I can parse the enclosure element.

Similar Threads

  1. QXmlStreamReader
    By sophister in forum Qt Programming
    Replies: 6
    Last Post: 24th August 2011, 17:31
  2. Mysterious QXMLStreamReader parsing behavior
    By ym1206 in forum Qt Programming
    Replies: 3
    Last Post: 14th June 2010, 19:56
  3. Possilble Bug? QXmlStreamReader
    By dempsey001 in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2010, 18:46
  4. Need help with QXmlStreamReader
    By jknotzke in forum Qt Programming
    Replies: 2
    Last Post: 26th October 2009, 09:26
  5. QXmlStreamReader issue
    By yagabey in forum Qt Programming
    Replies: 11
    Last Post: 31st December 2008, 21:06

Tags for this Thread

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.