/    Sign up×
Community /Pin to ProfileBookmark

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 ([url]http://codeamber.org/a1xl04act/amberalert.xml)[/url], 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>[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@toicontienNov 07.2007 — The browser is probably blocking you from loading remote XML files, or in the very least, XML files not coming from the same domain as your web page. There really is no workaround. If codeamber.org has a JavaScript feed reader, use that on your page, otherwise you're SOL. You also might be able to use a PHP script to fetch the XML file, then have PHP send your page the XML via an AJAX request. That might be a possible workaround.
Copy linkTweet thisAlerts:
@kwilliamsauthorNov 07.2007 — Hi,

Actually, 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>[/CODE]


And here's what the problem was:
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
  • [/QUOTE]


    Thanks for your help though.
    Copy linkTweet thisAlerts:
    @toicontienNov 07.2007 — Good info. I'll have to pack that away in the recesses of my mind for the future. ?
    Copy linkTweet thisAlerts:
    @kwilliamsauthorDec 18.2007 — Well, this is gone in a different direction thanks to everyone's help, and now I have another question:

    The code in this thread works great at pulling nodes from an external XML feed. I have an internal client-side "news scroller" script (JavaScript) that I want to display some of the nodes from the server-side code.

    So can I pull XML nodes from an external XML feed, declare variables from that code, and then use those server-side variables within client-side code?

    Something like this:
    &lt;%@ LANGUAGE="JAVASCRIPT" %&gt;
    &lt;%
    var xmlDoc;
    xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0")
    xmlDoc.async=false;
    xmlDoc.setProperty("ServerHTTPRequest", true) &lt;!-- You need this for both to get a remote XML feed this way --&gt;


    <i> </i>if (xmlDoc.load("http://codeamber.org/a1xl04act/amberalert.xml") == false)
    <i> </i>{
    <i> </i> Response.Write("Failed to load xml");
    <i> </i>}
    var amberDescription = xmlDoc.selectSingleNode("//FullText").text;//&lt;&lt;----CAN I PULL THIS INTO CLIENT-SIDE CODE???

    %&gt;
    &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Pull XML Feed&lt;/title&gt;
    &lt;script type="text/javascript" language="javascript"&gt;
    var pausecontent=new Array()

    //These need to be dynamically pulled from existing XML feeds:
    pausecontent[0]='&lt;strong&gt;&lt;a href="amberalert.aspx"&gt;Amber Alert:&lt;/a&gt;&lt;/strong&gt; ' + amberDescription + ' &lt;a href="amberdetails.aspx"&gt;Learn more &amp;#62;&amp;#62;&lt;/a&gt;'
    pausecontent[1]='&lt;strong&gt;&lt;a href="anotherfeed.aspx"&gt;Another Feed:&lt;/a&gt;&lt;/strong&gt; ' + anotherfeedDescription + ' &lt;a href="anotherfeed.aspx"&gt;Learn more &amp;#62;&amp;#62;&lt;/a&gt;'
    //.....MORE JS CODE GOES HERE TO CREATE THE ACTUAL SCROLLER...
    }
    &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;h1&gt;Pull XML Feed&lt;/h1&gt;
    &lt;p&gt;&lt;b&gt;Alertstatus:&lt;/b&gt; &lt;span id="Alertstatus"&gt;&lt;%=xmlDoc.selectSingleNode("//Alertstatus").text %&gt;&lt;/span&gt;&lt;br /&gt;
    &lt;b&gt;FullText:&lt;/b&gt; &lt;span id="FullText"&gt;&lt;%=xmlDoc.selectSingleNode("//FullText").text %&gt;&lt;/span&gt;&lt;br /&gt;
    &lt;b&gt;Alertinfo:&lt;/b&gt; &lt;span id="Alertinfo"&gt;&lt;/span&gt;&lt;br /&gt;
    &lt;b&gt;AlertState:&lt;/b&gt; &lt;span id="AlertState"&gt;&lt;%=xmlDoc.selectSingleNode("//Alertinfo/@states").text %&gt;&lt;/span&gt;&lt;br /&gt;
    &lt;b&gt;Victim:&lt;/b&gt; &lt;span id="Victim"&gt;&lt;%=xmlDoc.selectSingleNode("//Victim/@name").text %&gt;&lt;/span&gt;
    &lt;/p&gt;
    &lt;script type="text/javascript"&gt;
    new pausescroller(pausecontent, "scroller", "someclass", 8000)
    &lt;/script&gt;
    &lt;/body&gt;
    &lt;/html&gt;


    I don't know much about using client-side code in conjunction with server-side code, so if anyone can let me know if this is possible, that would be great. Thanks.
    ×

    Success!

    Help @kwilliams spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...