2 Star 7 Fork 3

softxing / OCAuxiliaryTools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
plistparser.cpp 3.13 KB
一键复制 编辑 原始数据 按行查看 历史
ic005k 提交于 2020-12-08 21:17 . Add files via upload
// Own includes
#include "plistparser.h"
// Qt includes
#include <QDateTime>
#include <QDebug>
#include <QDomDocument>
#include <QDomNode>
QVariant PListParser::parsePList(QIODevice* device)
{
QVariantMap result;
QDomDocument doc;
QString errorMessage;
int errorLine;
int errorColumn;
bool success = doc.setContent(device, false, &errorMessage, &errorLine, &errorColumn);
if (!success) {
qDebug() << "PListParser Warning: Could not parse PList file!";
qDebug() << "Error message: " << errorMessage;
qDebug() << "Error line: " << errorLine;
qDebug() << "Error column: " << errorColumn;
return result;
}
QDomElement root = doc.documentElement();
if (root.attribute(QStringLiteral("version"), QStringLiteral("1.0")) != QLatin1String("1.0")) {
qDebug() << "PListParser Warning: plist is using an unknown format version, parsing might fail unexpectedly";
}
return parseElement(root.firstChild().toElement());
}
QVariant PListParser::parseElement(const QDomElement& e)
{
QString tagName = e.tagName();
//qDebug() << tagName;
QVariant result;
if (tagName == QLatin1String("dict")) {
result = parseDictElement(e);
} else if (tagName == QLatin1String("array")) {
result = parseArrayElement(e);
} else if (tagName == QLatin1String("string")) {
result = e.text();
} else if (tagName == QLatin1String("data")) {
result = QByteArray::fromBase64(e.text().toUtf8());
} else if (tagName == QLatin1String("integer")) {
//result = e.text().toInt();
result = e.text().toLongLong(); //特别注意:原来的Int有点小,没法显示长数字
}
else if (tagName == QLatin1String("real")) {
result = e.text().toFloat();
} else if (tagName == QLatin1String("true")) {
result = true;
} else if (tagName == QLatin1String("false")) {
result = false;
} else if (tagName == QLatin1String("date")) {
result = QDateTime::fromString(e.text(), Qt::ISODate);
} else {
qDebug() << "PListParser Warning: Invalid tag found: " << e.tagName() << e.text();
}
return result;
}
QVariantList PListParser::parseArrayElement(const QDomElement& element)
{
QVariantList result;
QDomNodeList children = element.childNodes();
for (int i = 0; i < children.count(); i++) {
QDomNode child = children.at(i);
QDomElement e = child.toElement();
if (!e.isNull()) {
result.append(parseElement(e));
}
}
return result;
}
QVariantMap PListParser::parseDictElement(const QDomElement& element)
{
QVariantMap result;
QDomNodeList children = element.childNodes();
QString currentKey;
for (int i = 0; i < children.count(); i++) {
QDomNode child = children.at(i);
QDomElement e = child.toElement();
if (!e.isNull()) {
QString tagName = e.tagName();
if (tagName == QLatin1String("key")) {
currentKey = e.text();
} else if (!currentKey.isEmpty()) {
result[currentKey] = parseElement(e);
}
}
}
return result;
}
1
https://gitee.com/softxing/OCAuxiliaryTools.git
git@gitee.com:softxing/OCAuxiliaryTools.git
softxing
OCAuxiliaryTools
OCAuxiliaryTools
master

搜索帮助