如何使用javascript / jquery将xml文件加载到jsp / html页面

时间:2022-10-16 13:56:17

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.

我有一个在E盘E:\ xmlData.xml中创建的xml文件。我需要使用JavaScript / jQuery在页面加载时将此xml文件加载到jsp / html文件中。

3 个解决方案

#1


1  

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.

一般来说,JavaScript是客户端技术。它无权访问您的本地文件系统,您无法从那里加载文件。但HTML5提供了文件系统API。它可以让您解决您的问题。这是一个例子。

#2


0  

Create a Rest API based server. which will return the xml when the request is given by an Ajax call.

创建基于Rest API的服务器。当Ajax调用给出请求时,它将返回xml。

#3


0  

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.

jQuery提供了一个$ .get方法,可以从URL中捕获数据。因此,要“读取”xml文档,需要通过URL访问它。要使用jQuery,您必须从http://jquery.com/下载jQuery javascript库并将其放在当前项目/解决方案文件中。

use this inside the head tag :

在head标签内使用:

<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...

这是一个示例脚本,它将在加载时提醒xml文件内容...

<script type="text/javascript">
    var localFileUrl = '/Yourdirectory/file.xml';

    $(document).ready(function() {
        $.get(localFileUrl , function(fileContent) {
            alert(fileContent);
        });
    });
</script>

#1


1  

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.

一般来说,JavaScript是客户端技术。它无权访问您的本地文件系统,您无法从那里加载文件。但HTML5提供了文件系统API。它可以让您解决您的问题。这是一个例子。

#2


0  

Create a Rest API based server. which will return the xml when the request is given by an Ajax call.

创建基于Rest API的服务器。当Ajax调用给出请求时,它将返回xml。

#3


0  

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.

jQuery提供了一个$ .get方法,可以从URL中捕获数据。因此,要“读取”xml文档,需要通过URL访问它。要使用jQuery,您必须从http://jquery.com/下载jQuery javascript库并将其放在当前项目/解决方案文件中。

use this inside the head tag :

在head标签内使用:

<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...

这是一个示例脚本,它将在加载时提醒xml文件内容...

<script type="text/javascript">
    var localFileUrl = '/Yourdirectory/file.xml';

    $(document).ready(function() {
        $.get(localFileUrl , function(fileContent) {
            alert(fileContent);
        });
    });
</script>