3

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.

6
  • Which browser are you using? Where do you read the addess.xml? I only see a request for sample.htm Commented Aug 27, 2016 at 11:46
  • do you get any useful error messages in the browser developer tools console? can you verify, using the browser developer tools network tool that the XML is indeed being sent properly Commented Aug 27, 2016 at 11:48
  • is the request being made over http/https? Commented Aug 27, 2016 at 11:49
  • I have tried it on a number of browsers like chrome, firefox, opera.. Both the files are on my local system inside a folder "xml". Commented Aug 27, 2016 at 12:04
  • don't do "/xml/address.xml" ... use "xml/address.xml" ... but it probably will never work in chrome if you are not using http or https (i.e you've loaded the file using file:///) Commented Aug 27, 2016 at 12:08

3 Answers 3

1

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);
  }
Sign up to request clarification or add additional context in comments.

Comments

0

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>
    

7 Comments

what is the variable txt? what is xml/sample.htm? it's not even valid javascript!!! neither of these relate to the question
txt is a sample variable that have xml data. wait i am updating
why would I debug your answer?
@JaromandaX, you don't required to debug my code. you can see live here.
I don't care, it doesn't resolve the problem, you're not even getting the XML via XMLHttpRequest .. that's where the problem in the question is, I can almost guarantee it (they are using a page loaded using file:/// - this wont work in chrome ever, and unless the xml folder is a sub folder of the root, then /xml/whatever will not be found)
|
0

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: enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.