Example serializing object to XML

Discussion forum for contributors and developers who are using the QCAD version 3 ECMAScript development platform or the C++ plugin interface or who are otherwise looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Example serializing object to XML

Postby hungerburg » Thu Sep 15, 2011 10:52 am

If one wants to persist some data to disk, the qt json serializer is most useful. Sometimes one may want to store some data in xml, below code can be useful then, that I want to share. Its very simple and might not handle everybodies requirements though.

Code: Select all
/** Objekt in XML Datei schreiben.
 * Erwartet wird ein Objekt mit nur einem Unterobjekt, nämlich
 * der Wurzel des zu schreibenden XML Baums:
 *   writeXML("foo.xml", { "bar": {} });
 * ergibt also: <bar></bar>
 * Objekte deren Namen mit "@" beginnt werden als Attribute angesehen.
 * Felder werden zu einer Liste gleichnamiger Elemente zusammgefasst.
 *
 * @param {string} fn Name der Datei
 * @param {object} O Objekt
 * @returns {boolean} Erfolg
 */
MyXmlWriter.prototype.writeXml = function(fn, O) {

   /** Rekursion.
    * @param {Object|string} o Daten des Knoten
    * @param {string} name Name des Knoten
    * @requires doc XML-Dokument
    * @returns {Object} neuer Knoten
    */
   var walk = function (o, name) {
      // Felder als same name siblings ausgeben
      if (o instanceof Array) {
         var frag = doc.createDocumentFragment();
         for (var i = 0, n = o.length; i < n; i++) {
            frag.appendChild(walk(o[i], name));
         }
         return frag;
      }
      var el = doc.createElement(name);
      if (typeof o === "object") {
         for (var i in o) {
            if (i.charAt(0) === "@") { // Attribute erkennen
               el.setAttribute(i.substr(1), o[i]);
            } else {
               el.appendChild(walk(o[i], i));
            }
         }
      } else { // wird schon TEXT sein
         el.appendChild(doc.createTextNode(o));
      }
      return el;
   };

   // XML erzeugen
   var doc = new QDomDocument();
   var instr = doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
   doc.appendChild(instr);
   var about = doc.createComment("Created by MyQcadWizard at " + new Date());
   doc.appendChild(about);
   for (var i in O) {
      doc.appendChild(walk(O[i], i));
   }

   // XML in Datei schreiben
   var fh = new QFile(fn)
   if (!fh.open(QIODevice.OpenMode(QIODevice.WriteOnly, QIODevice.Text))) {
      return false;
   }
   var out = new QTextStream(fh);
   out.setCodec("UTF-8");
   out.writeString(doc.toString());
   out.flush();
   fh.close();
   return true;
}

/** Objekt erstellen und speichern. */
MyXmlWriter.prototype.test = function() {

   var X = {};
   X["@xmlns"] = "http://foo/bar";
   X["@id"] = "test";

   X.feld = [];
   X.feld.push({
      "name": "A",
      "wert": "eins"
   });
   X.feld.push({
      "name": "B",
      "wert": "zwei"
   });

   return this.writeXml("test.xml", { "root": X });
}
User avatar
hungerburg
Senior Member
 
Posts: 117
Joined: Fri May 28, 2010 7:35 pm

Return to QCAD 3 Developers and Contributors

Who is online

Users browsing this forum: No registered users and 1 guest

cron