user2281107
user2281107

Reputation: 319

Load xml file using javascript

Nothing gets printed on the web page when I try to open an HTML file having javascript. The script inside the html code loads the xml file and tries to print some element data. I have pasted the code below. Sadly no data of file gets printed. I have used browsers IE8 and Chrome. Please let me know what is the issue.

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

Upvotes: 4

Views: 45976

Answers (1)

Amith
Amith

Reputation: 1434

You can't open a local file using ajax

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

this can't be performed because JavaScript running in a web browser does not have access to the local filesystem. That would a huge security risk.

Upvotes: 4

Related Questions