Last active
December 28, 2022 19:44
-
-
Save pcolby/6558910 to your computer and use it in GitHub Desktop.
Sample function for parsing a QXmlStreamReader (or part thereof) to a QVariant tree.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QVariantMap xmlStreamToVariant(QXmlStreamReader &xml, const QString &prefix = QLatin1String("."), const int maxDepth = 1024) | |
{ | |
if (maxDepth < 0) { | |
qWarning() << QObject::tr("max depth exceeded"); | |
return QVariantMap(); | |
} | |
if (xml.hasError()) { | |
qWarning() << xml.errorString(); | |
return QVariantMap(); | |
} | |
if (xml.tokenType() == QXmlStreamReader::NoToken) | |
xml.readNext(); | |
if ((xml.tokenType() != QXmlStreamReader::StartDocument) && | |
(xml.tokenType() != QXmlStreamReader::StartElement)) { | |
qWarning() << QObject::tr("unexpected XML tokenType %1 (%2)") | |
.arg(xml.tokenString()).arg(xml.tokenType()); | |
return QVariantMap(); | |
} | |
QVariantMap map; | |
if (xml.tokenType() == QXmlStreamReader::StartDocument) { | |
map.insert(prefix + QLatin1String("DocumentEncoding"), xml.documentEncoding().toString()); | |
map.insert(prefix + QLatin1String("DocumentVersion"), xml.documentVersion().toString()); | |
map.insert(prefix + QLatin1String("StandaloneDocument"), xml.isStandaloneDocument()); | |
} else { | |
if (!xml.namespaceUri().isEmpty()) | |
map.insert(prefix + QLatin1String("NamespaceUri"), xml.namespaceUri().toString()); | |
foreach (const QXmlStreamAttribute &attribute, xml.attributes()) { | |
QVariantMap attributeMap; | |
attributeMap.insert(QLatin1String("Value"), attribute.value().toString()); | |
if (!attribute.namespaceUri().isEmpty()) | |
attributeMap.insert(QLatin1String("NamespaceUri"), attribute.namespaceUri().toString()); | |
if (!attribute.prefix().isEmpty()) | |
attributeMap.insert(QLatin1String("Prefix"), attribute.prefix().toString()); | |
attributeMap.insert(QLatin1String("QualifiedName"), attribute.qualifiedName().toString()); | |
map.insertMulti(prefix + attribute.name().toString(), attributeMap); | |
} | |
} | |
for (xml.readNext(); (!xml.atEnd()) && (xml.tokenType() != QXmlStreamReader::EndElement) | |
&& (xml.tokenType() != QXmlStreamReader::EndDocument); xml.readNext()) { | |
switch (xml.tokenType()) { | |
case QXmlStreamReader::Characters: | |
case QXmlStreamReader::Comment: | |
case QXmlStreamReader::DTD: | |
case QXmlStreamReader::EntityReference: | |
map.insertMulti(prefix + xml.tokenString(), xml.text().toString()); | |
break; | |
case QXmlStreamReader::ProcessingInstruction: | |
map.insertMulti(prefix + xml.processingInstructionTarget().toString(), | |
xml.processingInstructionData().toString()); | |
break; | |
case QXmlStreamReader::StartElement: | |
map.insertMulti(xml.name().toString(), xmlStreamToVariant(xml, prefix, maxDepth-1)); | |
break; | |
default: | |
qWarning() << QObject::tr("unexpected XML tokenType %1 (%2)") | |
.arg(xml.tokenString()).arg(xml.tokenType()); | |
} | |
} | |
return map; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@chakani, oops.
toVariant
was the original name I gave the function... ie its recursive. I'll fix that up now (only three years late 😆)