Skip to content Skip to sidebar Skip to footer

Letting User Open An Xml File On Client And Parse It Using Javascript

I'm trying to let a users on my site to save and XML file one the local machine and then later to load them using the HTML file element. Saving the file what done with iFrame. When

Solution 1:

I think you're looking for FileReader, new in HTML5. For IE < 10 you'll need to use the ActiveX FileSystemObject.

This code works for me on Chrome.

<script type="text/javascript">
function doit(e) {
  var files = e.target.files;
  var reader = new FileReader();
  reader.onload = function() {
    var parsed = new DOMParser().parseFromString(this.result, "text/xml");
    console.log(parsed);
  };
  reader.readAsText(files[0]);
}

document.getElementById("selectfile").addEventListener("change", doit, false);​
</script>

<input type="file" id="selectfile" />

http://jsfiddle.net/xKuPV/


Post a Comment for "Letting User Open An Xml File On Client And Parse It Using Javascript"