PDA

View Full Version : Parsing RSS 2.0 using QXmlStreamReader



matsukan
29th August 2011, 07:12
Hi

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


<enclosure url="http://www.w3schools.com/media/3d.wmv" length="78645" type="video/wmv" />

This is my xml parsing routine:


while (!m_xml.atEnd()) {
m_xml.readNext();
if (m_xml.isStartElement()) {
m_currentTag = m_xml.name().toString();
} else if (m_xml.isEndElement()) {
if (m_xml.name() == "item") {
if (!m_feeds.contains(feedItem)) {
feeds.append(feedItem);
newFeeds = true;
}
feedItem.m_category.clear();
feedItem.m_title.clear();
feedItem.m_link.clear();
feedItem.m_quid.clear();
feedItem.m_description.clear();
}
} else if (m_xml.isCharacters() && !m_xml.isWhitespace()) {
if (m_currentTag == "title") {
feedItem.m_title += m_xml.text().toString();
}
else if (m_currentTag == "link") {
feedItem.m_link += m_xml.text().toString();
}
else if (m_currentTag == "category") {
feedItem.m_category += m_xml.text().toString();
}
else if (m_currentTag == "pubDate") {
convertPublishDate(feedItem.m_pubDate, m_xml.text().toString());
}
else if (m_currentTag == "description") {
feedItem.m_description += m_xml.text().toString();
}
else if (m_currentTag == "guid") {
feedItem.m_quid += m_xml.text().toString();
} else if (m_currentTag == "enclosure") {
qDebug () << m_xml.text().toString();
}
} else if (m_xml.isComment())
qDebug () << m_xml.text().toString();

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

thanks,

ChrisW67
29th August 2011, 08:32
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.

matsukan
29th August 2011, 11:51
thanks to tip !

I did little modifications to my code :

while (!m_xml.atEnd()) {
m_xml.readNext();
if (m_xml.isStartElement()) {
m_currentTag = m_xml.name().toString();
qDebug () << "start " << m_currentTag;
if (m_xml.name() == "enclosure" && newFeeds) {
feedItem.m_enclosure.m_url = m_xml.attributes().value("url").toString();
feedItem.m_enclosure.m_type = m_xml.attributes().value("type").toString();
}
} else if (m_xml.isEndElement()) {
if (m_xml.name() == "item") {


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