Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
| Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 7th, 2007, 01:47 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems parsing XML Feed in Javascript

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
  #2 (permalink)  
Old November 7th, 2007, 05:59 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

FYI: I just received a workaround from another forum without the need of an external JS feed reader. Here's the code:
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>
</head>

<%
    var xmlDoc;
    xmlDoc = new ActiveXObject("MSXML2.DOMDocument.6.0")
       xmlDoc.async=false;
    xmlDoc.setProperty("ServerHTTPRequest", true) 


    if (xmlDoc.load("http://codeamber.org/a1xl04act/amberalert.xml") == false)
    {
        Response.Write("Failed to load xml");
    }

%>

<body>
    <h1>Pull XML Feed</h1>
    <p><b>Alertstatus:</b> <span id="Alertstatus"><%=xmlDoc.selectSingleNode("//Alertstatus").text %></span><br />
    <b>FullText:</b> <span id="FullText"><%=xmlDoc.selectSingleNode("//FullText").text %></span><br />
    <b>Alertinfo:</b> <span id="Alertinfo"></span><br />
    <b>AlertState:</b> <span id="AlertState"><%=xmlDoc.selectSingleNode("//Alertinfo/@states").text %></span><br />
    <b>Victim:</b> <span id="Victim"><%=xmlDoc.selectSingleNode("//Victim/@name").text %></span>
    </p>
</body>

</html>
And here's what the problem was:
Quote:
quote:There are a few things here worth considering

1. I noticed <%@ LANGUAGE="JAVASCRIPT @> in your first line. This tells me you are using an ASP page, but then you push the parsing to the client. Why not let the server do the work? You're going to have all kinds of problems with rendering XML at the client, not the least being security and permission to download parsers, etc...

2. Microsoft.XMLDOM parser has been deprecated for several years. That was version 0.5. It was only able to do 1/2 the ability of the spec. It lacked support for XSLT processing (it uses XSL). You should upgrade to version 6.0

3. I was able to get this too run in my IE 7.0 as an HTML page. So there is no problem with loading the remote XML.

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
alert(xmlDoc.load("http://codeamber.org/a1xl04act/amberalert.xml"))
alert(xmlDoc.xml)

4. Try rewriting as an ASP page, its not that hard. No need to check for browser type as the server will be doing the work. Hope that this helps a little. Following code works just fine for me using IIS 5.0 and IE 7.0
Thanks anyway:)

KWilliams


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET XML Feed Error kwilliams ASP.NET 2.0 Professional 0 February 19th, 2008 05:41 PM
Problems parsing XML Feed in Javascript kwilliams Classic ASP Professional 0 December 18th, 2007 06:33 PM
Parsing XML with Javascript rvanandel Javascript How-To 2 August 29th, 2005 09:37 AM
xml feed with javascript jon8884 Javascript 5 June 22nd, 2005 01:15 PM
XML news feed sankar XML 1 December 17th, 2003 05:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.