简体   繁体   中英

Cannot parse xml data with javascript HTML DOM on local system

I tried this sample code from tutorialspoint website on a number of browsers. But the xml data is not being parsed. Both these files are on my local system and the address.xml file is inside a folder "xml".

How can I parse data in javascript from the xml files on my local system?

Here is my HTML file sample.html from tutorialspoint website:

<!DOCTYPE html>
<html>
   <body>
      <h1>TutorialsPoint DOM example </h1>
      <div>
         <b>Name:</b> <span id="name"></span><br>
         <b>Company:</b> <span id="company"></span><br>
         <b>Phone:</b> <span id="phone"></span>
      </div>
      <script>
         if (window.XMLHttpRequest)
         {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
         }
         else
         {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.open("GET","/xml/address.xml",false);
         xmlhttp.send();
         xmlDoc=xmlhttp.responseXML;

         document.getElementById("name").innerHTML=
         xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
         document.getElementById("company").innerHTML=
         xmlDoc.getElementsByTagName("company")[0].childNodes[0].nodeValue;
         document.getElementById("phone").innerHTML=
         xmlDoc.getElementsByTagName("phone")[0].childNodes[0].nodeValue;
      </script>
   </body>
</html>

This is the xml data file address.xml:

<?xml version="1.0"?>
<contact-info>
   <name>Tanmay Patil</name>
   <company>TutorialsPoint</company>
   <phone>(011) 123-4567</phone>
</contact-info>

Update: The problem was with getting an http response on local system. It is solved after I installed XAMPP.

use parsing like the following

    if (window.DOMParser)
  {
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(xmlhttp.responseXML, "text/xml");
  }
else // Internet Explorer
  {
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    xmlDoc.loadXML(xmlhttp.responseXML);
  }

This example of XML parsing, if you have xml value in variable.

 Sample = "<contact-info><name>Tanmay Patil</name><company>TutorialsPoint</company><phone>(011) 123-4567</phone></contact-info>"; if (window.DOMParser) { parser = new DOMParser(); xmlDoc = parser.parseFromString(Sample, "text/xml"); } document.getElementById("name").innerHTML= xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue; document.getElementById("company").innerHTML= xmlDoc.getElementsByTagName("company")[0].childNodes[0].nodeValue; document.getElementById("phone").innerHTML= xmlDoc.getElementsByTagName("phone")[0].childNodes[0].nodeValue; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <b>Name:</b> <span id="name"></span><br> <b>Company:</b> <span id="company"></span><br> <b>Phone:</b> <span id="phone"></span> 

As Jaromanda X mentioned in the comments, and as I found out the hard way, Chrome won't parse local xml files using the DOM parser. However, if you directly use the console and save your xml text there (as a var/let,etc..) you will be able to parse it using the DOM parser.

Ex: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM