How to display output in plist format. Is it possible or not? and also the data in the textarea should be able to load?

<html>
<head></head>
<body>
      <textarea id="area" style="height:200px;width:80%">
          Hello Everyone.
          Entry name 2.
          rahul
          mahesh</textarea>
<button id="submit">Submit</button>

 <script>
 function pad(num, diff) {
    return diff > 0 ? pad(num, diff - 1) + 0 : num;
 }

 function processString(num) {
    var tmp = num.replace(/\.+/g, '');
    var diff = 4 - tmp.length;
    var tmpl = '<string>#{padded}</string>';
    var padded = diff > 0 ? pad(tmp, diff).split('').join('.') : num;
    return tmpl.replace('#{padded}', padded);
  }

  var button = document.getElementById('submit');
  button.addEventListener('click', processArea);

  function processArea() {
     var area = document.getElementById('area')
     var arr = area.value.split(/\n/);
     for (var i = 0, l = arr.length; i < l; i++) {
     arr[i] = processString(arr[i]);
   }
   area.value = arr.join('\r\n');
}

</script>
</body>
</html>

      

See http://jsfiddle.net/1csov2s1/15/

From the top of the Fiddle, it generates the list correctly, but I need the generated List to be in .plist format.

Can someone help me.

expected output:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library
/DTDs/PropertyList.dtd">
<plist version="1.0">
<dict>
<array>
<string>Hello Everyone.</string>
<string>Entry name 2.</string>
<string>rahul</string>
<string>mahesh</string>
</array>
</plist>

      

Current output:

<string>Hello Everyone.</string>
<string>Entry name 2.</string>
<string>rahul</string>
<string>mahesh</string>

      

Any help would be appreciated.

+3


source to share


1 answer


You can add header and footer as inline syntax as follows. You can call a function saveTextAsFile

to load. Tested on chrome.

function processArea() {
    var area = document.getElementById('area');
    var arr = area.value.split(/\n/);
    var header = '<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">\n<plist version="1.0">\n<dict>\n<array>';
    var footer = '\n</array>\n</plist>';
    for (var i = 0, l = arr.length; i < l; i++) {
      arr[i] = processString(arr[i]);
    }
    area.value = header+arr.join('\r\n')+footer;
}

function saveTextAsFile()
{
    var textToWrite = document.getElementById('area').value;
    var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
    var fileNameToSaveAs = "ecc.plist";/*Your file name*/

    var downloadLink = document.createElement("a");
    downloadLink.download = fileNameToSaveAs;
    downloadLink.innerHTML = "Download File";
    if (window.webkitURL != null)
    {
        // Chrome allows the link to be clicked
        // without actually adding it to the DOM.
        downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
    }
    else
    {
        // Firefox requires the link to be added to the DOM
        // before it can be clicked.
        downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
        downloadLink.onclick = destroyClickedElement;
        downloadLink.style.display = "none";
        document.body.appendChild(downloadLink);
    }

    downloadLink.click();
}

      



JSFiddle

+3


source







All Articles