Skip to content

Instantly share code, notes, and snippets.

@pcolby
Last active December 28, 2022 19:44
Show Gist options
  • Save pcolby/6558910 to your computer and use it in GitHub Desktop.
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.
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;
}
@pcolby
Copy link
Author

pcolby commented Sep 14, 2013

I originally wrote this as part of a libqtaws (AWS SDK for Qt) library I'm working on. But ended up not using it (yet).

@chakani
Copy link

chakani commented Sep 22, 2018

What is 'toVariant'?

@pcolby
Copy link
Author

pcolby commented Sep 8, 2021

@chakani, oops. toVariant was the original name I gave the function... ie its recursive. I'll fix that up now (only three years late 😆)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment