我正在使用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;此致
答案 0 :(得分:0)
XmlDataAccess::load
的返回值为QVariant
,其中包含QVariantList
,QVariantMap
或QString
。支持的转换相对简单(如详细here),目的是保留数据结构,而不是特定的XML结构。将parsedresponselist的内容转储到qDebug()并开始从那里开始挑选它可能会有所帮助。
我现在通常使用QVariant::canConvertTo<QVariantMap>()
和QVariant::canConvertTo<QVariantList>()
来确定返回哪种变体,然后遍历树遍历列表中的项目,或者遍历地图中的键,或者在我找到我正在寻找的数据之前使用其他访问者。