最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

html - Load xml file using javascript - Stack Overflow

matteradmin2PV0评论

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>

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>
Share edited Aug 17, 2013 at 19:33 Jaak Kütt 2,6564 gold badges33 silver badges40 bronze badges asked Aug 17, 2013 at 19:24 user2281107user2281107 3192 gold badges5 silver badges14 bronze badges 2
  • 1 You're definitely not going to get any joy with Chrome since you're using ActiveX. Any chance you can build a web service to surface that XML file? – Mister Epic Commented Aug 17, 2013 at 20:24
  • any errors in your console? – metadings Commented Aug 17, 2013 at 20:25
Add a ment  | 

1 Answer 1

Reset to default 4

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.

Post a comment

comment list (0)

  1. No comments so far