簡體   English   中英

使用javascript加載xml文件

[英]Load xml file using javascript

當我嘗試打開具有javascript的HTML文件時,網頁上沒有打印任何內容。 html代碼中的腳本加載xml文件並嘗試打印一些元素數據。 我已粘貼下面的代碼。 遺憾的是,沒有打印文件數據。 我使用過瀏覽器IE8和Chrome。 請告訴我這是什么問題。

<!DOCTYPE html>
<html>
    <head>
        <script>
            function loadXMLDoc(dname)
            {
                if (window.XMLHttpRequest)
                {
                    xhttp=new XMLHttpRequest();
                }
                else
                {
                    xhttp=new ActiveXObject("Microsoft.XMLDOM");
                }

                xhttp.open("GET",dname,false);
                xhttp.send();
                return xhttp.responseXML;
            }
        </script>
    </head>
    <body>
        <script>
            xmlDoc=loadXMLDoc("file:///E:/Parser/book.xml");

            document.write(xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue + "   <br>");
            document.write(xmlDoc.getElementsByTagName("authors")[0].childNodes[0].nodeValue + "<br>");

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

您無法使用ajax打開本地文件

xmlDoc=loadXMLDoc("file:///E:/Parser/book.xml"); 

這是無法執行的,因為在Web瀏覽器中運行的JavaScript無法訪問本地文件系統。 這將帶來巨大的安全風險。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM