使用QVariant进行XMLDataAccess

时间:2014-07-26 08:20:56

标签: blackberry blackberry-10 blackberry-cascades

我正在使用XMLData访问来创建xml文件。

以下是创建新xml文件的代码

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;

我也在编写相应的读取代码,我也在使用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");

    }

现在问题是如何从parsedresponselist

中检索更多元素

请建议

谢谢&amp;此致

1 个答案:

答案 0 :(得分:0)

XmlDataAccess::load的返回值为QVariant,其中包含QVariantListQVariantMapQString。支持的转换相对简单(如详细here),目的是保留数据结构,而不是特定的XML结构。将parsedresponselist的内容转储到qDebug()并开始从那里开始挑选它可能会有所帮助。

我现在通常使用QVariant::canConvertTo<QVariantMap>()QVariant::canConvertTo<QVariantList>()来确定返回哪种变体,然后遍历树遍历列表中的项目,或者遍历地图中的键,或者在我找到我正在寻找的数据之前使用其他访问者。