var XMLStorage = "";

// Structure to SVG output

function domSub(d, flag) {
    for(var x in d) {
      if(d[x] instanceof Object || typeof d[x] == "object")  continue;
      if(x=="tag") continue;
      if(x.slice(0,2) === "_0") continue;
      if(x == "data") continue;
      XMLStorage += " " + x + "=\""+ d[x] + "\"";
      flag=true;
    }
    for(var x in d) {
      if(d[x] instanceof Object || typeof d[x] == "object") {
        var tagKey = d[x]["tag"]
        if(flag) XMLStorage += ">\n";
        XMLStorage += "<" + tagKey;
        if(x.slice(0,2) != "_0") XMLStorage += " id=\"" + x + "\"";
        if( domSub(d[x], true) )
            XMLStorage += ">";
        XMLStorage += "</" + tagKey + ">\n";
        flag = false;
        continue;
      }
      if(x == "data") {
        XMLStorage += ">" + d[x];
        flag = false;
        continue;
      }
    }
  return flag;
}


exports.toSVG=function(d) {
  XMLStorage = "";
  var d2 = {}
  var id = d["_00"];
  d2["_000"] = Object.create(d);
  d2["_000"]["id"] = id;
  if(domSub(d2, false)) {
      XMLStorage += ">\n";
  }
  return XMLStorage;
}