I'm just learning how to parse an XML feed with JavaScript, but I'm having some problems. If I copy and paste the code from the XML feed into an internal file, and I reference that file in my code, it works fine. But when I try to change the XML source doc to the XML feed's URL (
http://codeamber.org/a1xl04act/amberalert.xml), nothing shows up. Obviously there's something that I'm missing when it comes to parsing an internal XML doc vs. an XML feed. In addition to pulling in the main elements, I'm trying to pull the attributes of some elements without success. So if anyone could let me know what I'm missing and/or doing wrong, it would be greatly appreciated. I've included my code below:
Code:
<%@LANGUAGE="JAVASCRIPT" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Pull XML Feed</title>
<script type="text/javascript">
var xmlDoc;
function loadXML()
{
//load xml file
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("pullxmlfeed.xml");//I want this to pull from external XML feed at http://codeamber.org/a1xl04act/amberalert.xml
getmessage();
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("pullxmlfeed.xml");//I want this to pull from external XML feed at http://codeamber.org/a1xl04act/amberalert.xml
xmlDoc.onload=getmessage;
}
else
{
alert('Your browser cannot handle this script');
}
}
function getmessage()
{
document.getElementById("Alertstatus").innerHTML=xmlDoc.getElementsByTagName("Alertstatus")[0].childNodes[0].nodeValue;
document.getElementById("FullText").innerHTML=xmlDoc.getElementsByTagName("FullText")[0].childNodes[0].nodeValue;
document.getElementById("Alertinfo").innerHTML=xmlDoc.getElementsByTagName("Alertinfo")[0].childNodes[0].nodeValue;
document.getElementById("AlertState").innerHTML=Alertinfo.getElementsByTagName("states")[0].childNodes[0].nodeValue;//I want to pull attribute 'status' from 'Alertinfo' node
document.getElementById("Victim").innerHTML=xmlDoc.getElementsByTagName("Victim")[0].childNodes[0].nodeValue;
}
</script>
</head>
<body onload="loadXML()">
<h1>Pull XML Feed</h1>
<p><b>Alertstatus:</b> <span id="Alertstatus"></span><br />
<b>FullText:</b> <span id="FullText"></span><br />
<b>Alertinfo:</b> <span id="Alertinfo"></span><br />
<b>AlertState:</b> <span id="AlertState"></span><br />
<b>Victim:</b> <span id="Victim"></span>
</p>
</body>
</html>
KWilliams