Reputation: 455
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.
Upvotes: 3
Views: 1426
Reputation: 377
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.
Upvotes: 0
Reputation: 1697
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>
Upvotes: 0
Reputation: 7015
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);
}
Upvotes: 1