       
       # If JSON element is just a string value
       else
-         $a.xml += ind + "<" + name + ">" + v.toString() +  "</" + name + ">"
+         $a.xml += ind + "<" + name + ">" + v?.toString() +  "</" + name + ">"
       $a.xml
 
-    for elm in json
-      xml += toXml(json[elm], elm, "");
+    for key of json
+      xml += toXml(json[key], key, "");
     if tab? 
       xml.replace(/\t/g, tab)
     else

  # Function to convert XML to JSON 
  # Reference: http://goessner.net/download/prj/jsonxml/
  json2xml: (json, tab) ->
    # Recursive function to transveres through child JSON objects
    toXml: (v, name, ind) ->
      # define XML variable to be global
      $a.xml = "";
      # If JSON element is an Array

      else if typeof(v) == "object" 
        hasChild = false
        $a.xml += ind + "<" + name
        for key of v
          # If element has "@" character at beginning it is an attribute
          if key.charAt(0) == "@"
            $a.xml += " " + key.substr(1) + "=\"" + v[key].toString() + "\""
