bool writeXmlFile(QIODevice &device, const QSettings::SettingsMap &map) { QXmlStreamWriter xmlWriter(&device); xmlWriter.setAutoFormatting(true); xmlWriter.writeStartDocument(); xmlWriter.writeStartElement("Settings"); QStringList prev_elements; QSettings::SettingsMap::ConstIterator map_i; // Alle Elemente der Map durchlaufen for (map_i = map.begin(); map_i != map.end(); map_i++) { QStringList elements = map_i.key().split("/"); int x = 0; // Zu schließende Elemente ermitteln while (x < prev_elements.size() && elements.at(x) == prev_elements.at(x)) { x++; } // Elemente schließen for (int i = prev_elements.size() - 1; i >= x; i--) { xmlWriter.writeEndElement(); } // Elemente öffnen for (int i = x; i < elements.size(); i++) { xmlWriter.writeStartElement(elements.at(i)); } // Wert eintragen xmlWriter.writeCharacters(map_i.value().toString()); prev_elements = elements; } // Noch offene Elemente schließen for (int i = 0; i < prev_elements.size(); i++) { xmlWriter.writeEndElement(); } xmlWriter.writeEndElement(); xmlWriter.writeEndDocument(); return true; }
static void writeGroup( const QString &groupKey, YAML::Emitter &out, SettingsIterator &it, const QSettings::SettingsMap &map ) { out << YAML::BeginMap; int groupKeyLen = groupKey.size(); while(it != map.end()) { QString key( it.key() ); if (!key.startsWith(groupKey)) break; int i_separ = key.indexOf("/", groupKeyLen); if (i_separ != -1) { // There is child nodes key.truncate( i_separ + 1 ); QString yamlKey(key); yamlKey.remove(0, groupKeyLen); yamlKey.chop(1); out << YAML::Key << yamlKey.toStdString(); out << YAML::Value; writeGroup( key, out, it, map ); } else { // There is no child nodes key.remove(0, groupKeyLen); out << YAML::Key << key.toStdString(); out << YAML::Value; writeValue( it.value(), out ); ++it; } } out << YAML::EndMap; }