Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 37
Registered: ‎09-04-2012
My Device: Blackberry 10 Dev Alpha
My Carrier: Vodafone Mumbai

XML Parsing using QXMLStreamReader

I am using QXmlStreamReader to parse the xml comming from this link.

 

I am not able to get the media:content element, below is the source code that i have created.

 

void Test::loadFeeds() {
	QNetworkAccessManager* netManager = new QNetworkAccessManager(this);
	qDebug() << "RssFeed::RssFeed::loadFeeds::\t" ;
	//const QUrl url("http://feeds.feedburner.com/androidcentral?format=xml");
	const QUrl url("http://feeds.feedburner.com/time/topstories?format=xml");

	QNetworkRequest request(url);

	QNetworkReply* reply = netManager->get(request);
	connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished()));
}

void Test::onReplyFinished() {
	qDebug() << "RssFeed::RssFeed::onReplyFinished::\t" ;
	QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());

	QString response;
	if (reply) {
		if (reply->error() == QNetworkReply::NoError) {
			qDebug() << "No error";
			const int available = reply->bytesAvailable();
			if (available > 0) {

				qDebug() << "fetching data";
				QByteArray data(reply->readAll());
				QList<QMap<QString, QVariant> > list = parseNetworkResponse(data);

			}else{
				qDebug() << "availabel was 0";
			}
		}else{
			qDebug() << "\n Problem with the network";
					qDebug() << "\n" << reply->errorString();
		}

	}
}

QList<QMap<QString, QVariant> > Test::parseNetworkResponse(
		QByteArray &data) {

	/* QXmlStreamReader takes any QIODevice. */
	QXmlStreamReader xml(data);
	QList<QMap<QString, QVariant> > feeds;

	/* We'll parse the XML until we reach end of it.*/
	while (!xml.atEnd() && !xml.hasError()) {
		/* Read next element.*/
		QXmlStreamReader::TokenType token = xml.readNext();
		/* If token is just StartDocument, we'll go to next.*/
		if (token == QXmlStreamReader::StartDocument) {
			continue;
		}
		/* If token is StartElement, we'll see if we can read it.*/
		if (token == QXmlStreamReader::StartElement) {
			/* If it's named persons, we'll go to the next.*/
			if (xml.name() == "channel") {
				continue;
			} else if (xml.name() == "title") {
				xml.readNext();
				if (xml.tokenType() != QXmlStreamReader::Characters) {

				} else {
					/* Now we can add the Title of the Feeds.*/
					//rssTitle(xml.text().toString());
					qDebug() << "Title: " << xml.text().toString();
				}
			} else if (xml.name() == "item") {
				/* If it's named person, we'll dig the information from there.*/
				feeds.append(this->parseInternalItems(xml));
			}
		}
	}
	/* Error handling. */
	if (xml.hasError()) {
//		showToast(
//				"Incomplete data was received, or else the network connection was lost while downloading of the data please try again.");
		qDebug() << "QXSRExample::parseXML " << xml.errorString();
	}
	/* Removes any device() or data from the reader
	 * and resets its internal state to the initial state. */
	xml.clear();
	qDebug() << "RssFeed::parseNetworkResponse::\t"
			<< "End retuning with size :: " << feeds.size();
	return feeds;
}

QMap<QString, QVariant> Test::parseInternalItems(QXmlStreamReader& xml) {
	QMap<QString, QVariant> person;

	/* Let's check that we're really getting a person. */
	if (xml.tokenType() != QXmlStreamReader::StartElement
			&& xml.name() == "person") {
		return person;
	} else if (xml.tokenType() != QXmlStreamReader::StartElement
			&& xml.name() == "PeopleDetail") {
		return person;
	}

//	/* We dont have any attributes to parser*/
//	/* Let's get the attributes for person */
//	QXmlStreamAttributes attributes = xml.attributes();
//	/* Let's check that person has id attribute. */
//	if (attributes.hasAttribute("id")) {
//		/* We'll add it to the map. */
//		person["id"] = attributes.value("id").toString();
//	}

	/* Next element... */
	xml.readNext();
	/*
	 * We're going to loop over the things because the order might change.
	 * We'll continue the loop until we hit an EndElement named person.
	 */
	while (!(xml.tokenType() == QXmlStreamReader::EndElement
			&& (xml.name() == "item"))) {
		if (xml.tokenType() == QXmlStreamReader::StartElement) {
			/* We've found first name. */
			
			if (
					(xml.name() == "title") || (xml.name() == "link")
					|| (xml.name() == "description") || (xml.name() == "pubDate")
					|| (xml.name()=="image") || (xml.name()=="media:content")
				){
				this->addElementDataToMap(xml, person);
			}
		}
		/* ...and next... */
		xml.readNext();
	}
	qDebug() << "========================================";
	return person;
}

void Test::addElementDataToMap(QXmlStreamReader& xml,
		QMap<QString, QVariant>& map) const {
	/* We need a start element, like <foo> */
	if (xml.tokenType() != QXmlStreamReader::StartElement) {
		return;
	}
	/* Let's read the name... */
	QString elementName = xml.name().toString();
	/* ...go to the next. */
	xml.readNext();
	/*
	 * This elements needs to contain Characters so we know it's
	 * actually data, if it's not we'll leave.
	 */
	if (xml.tokenType() != QXmlStreamReader::Characters) {
		return;
	}
	qDebug() << "Inserting :: " << elementName << " :: " << xml.text().toString();
	/* Now we can add it to the map.*/
	map.insert(elementName, xml.text().toString());
}

Please help me