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
New Developer
Posts: 37
Registered: ‎07-13-2014
My Device: Z10
My Carrier: Vodafone

Using QVariant for XMLDataAccess

[ Edited ]

Hi All,

 

I am using XMLData access for creating a xml file.

 

Following is the code for creating the new xml file

 

	if(myFile.open(QIODevice::WriteOnly)) {
		QVariantList messageList;
		XmlDataAccess xda;
		QDataStream out(&myFile);

		// Iterate through all the messages and convert it into data stream
		foreach (const Message &message, inboxMessages) {

			QVariantMap messageMap;

			//Adding sender Details to QVariantMap.
			messageMap["senderName"] = QVariant(message.sender().name());
			messageMap["addressSender"] = QVariant(message.sender().address());

			// There can be multiple recipients of mail so writing the recipient List
			QList<MessageContact> recipientList  = message.recipients();
			QVariantList recipientVariantList = QVariantList() ;
			foreach (const MessageContact &messageContact , recipientList) {
				QVariantMap recipientMap;
				recipientMap["recipientName"] = QVariant(messageContact.name());
				recipientMap["recipientAddress"] = QVariant(messageContact.address());
				recipientVariantList << QVariant(recipientMap);
			}
			messageMap["recipients"] = QVariant(recipientVariantList);
			// Other details of message like body,text etc.
			messageMap["date"] = QVariant(message.serverTimestamp().toString(Qt::TextDate));
			messageMap["subject"] = QVariant(message.subject());
			messageMap["body"] = QVariant(message.body(MessageBody::PlainText).plainText());

			// Adding this object to Qvariant List
			messageList = QVariantList() << QVariant(messageMap);

		}
		QVariantMap topLevelMessageMap;
		topLevelMessageMap[".root"] = QVariant("messages");
		topLevelMessageMap["message"] = QVariant(messageList);

		QVariant messageData = QVariant(topLevelMessageMap);
		xda.save(messageData, &myFile);
		successFlag = true;

 I am also writing its corresponding read code where also i am using XMLDataAccess

XmlDataAccess xda ;
QVariant parsedresponselist ; QFile myFile("/accounts/1000/removable/sdcard/messages/messages.xml"); if(myFile.open(QIODevice::ReadOnly)) { parsedresponselist = xda.load(&myFile,"/messages/message"); }

 

Now problem here is how to retrieve further elements from parsedresponselist

 

Please suggest

 

Thanks & Regards

Developer
Posts: 161
Registered: ‎01-03-2013
My Device: n/a
My Carrier: n/a

Re: Using QVariant for XMLDataAccess

XmlDataAccess xda ;
QVariant parsedresponselist ;
	QFile myFile("/accounts/1000/removable/sdcard/messages/messages.xml");

	if(myFile.open(QIODevice::ReadOnly)) {
		parsedresponselist = xda.load(&myFile,"/messages/message");
		QVariantList messageList = parsedresponselist.toList();
		for(int i = 0; i < messageList.size(); ++i)
		{
		  QVariantMap messageMap = messageList[i].toMap();
		  QMap<QString, QVariant>::iterator i = messageMap.begin();
		  while(i != messageMap.end())
		  {
		    do whatever you want with the values
		    qDebug() << "Key: " << i.key() << " value: " << i.value();
		  }
		}		  		
	}

 

Regards.

If my answer has been useful to you, please, click the button
Highlighted
New Developer
Posts: 37
Registered: ‎07-13-2014
My Device: Z10
My Carrier: Vodafone

Re: Using QVariant for XMLDataAccess

Thanks for the response . Well i have used same approach except some minor changes

 

parsedresponselist = xda.load(&myFile,"/messages/message");
		qVariantList = parsedresponselist.toList();
		//qVariantList = parsedresponselist.toList();
		QList<Message> parsedMessageList;
		foreach (QVariant listMember , qVariantList) {

			QVariantMap parsedMessageMap;
			parsedMessageMap      = 	listMember.toMap();
			QString senderName    = 	parsedMessageMap["senderName"].toString();
			QString senderAddress = 	parsedMessageMap["senderAddress"].toString();
			QString subject       = 	parsedMessageMap["subject"].toString();
			QString messageBody   = 	parsedMessageMap["body"].toString();
			QString date          = 	parsedMessageMap["date"].toString();

 

Please tell whats the difference between calling toList() and value<QVariantList>();

 

Also please tell if there is any error in code i have pasted. SInce i know the key Strings so i have used them as hardcoded to retrieve them from Map.

 

Please suggest.

 

Thanks & Regards

 

Developer
Posts: 161
Registered: ‎01-03-2013
My Device: n/a
My Carrier: n/a

Re: Using QVariant for XMLDataAccess

On the one hand, toValue<QVariantList> will try to convert the list  you pass to a QVariantList even if it's not a QVariantList or QStringList. On the other hand, toList() will check if the type of the QVariant is QVariantList or QStringList. If it's not either of them it will return an empty QVariantList.

 

As regards your code, it seems to be fine.

Regards.

If my answer has been useful to you, please, click the button