I have an xml file created in E drive E:\xmlData.xml.
I need to load this xml file into jsp/html file on page load using JavaScript/jQuery.
-
As far as I know, JavaScript can only read local files through a file-upload form.JCOC611– JCOC6112012-08-08 05:33:34 +00:00Commented Aug 8, 2012 at 5:33
-
@JCOC611: Can't we use some jquery ajax to get the file from remote location ie driversubbusaabu– subbusaabu2012-08-08 05:38:09 +00:00Commented Aug 8, 2012 at 5:38
3 Answers
Generally speaking JavaScript is client-side technology. It doesn't have access to your local file system and you can't load a file from there. But HTML5 provides the File System API. It allows you to solve your issue. Here's an example.
Comments
jQuery provides a method $.get which can capture the data from a URL. So to "read" the xml document, it needs to be accessible through a URL. for using jQuery you must download jQuery javascript library from http://jquery.com/ and put it on the current project/solution file.
use this inside the head tag :
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
This is an example script this will alert xml file content on load...
<script type="text/javascript">
var localFileUrl = '/Yourdirectory/file.xml';
$(document).ready(function() {
$.get(localFileUrl , function(fileContent) {
alert(fileContent);
});
});
</script>