/    Sign up×
Community /Pin to ProfileBookmark

Extracting Data from XML news feed

Trying to extract and display data from an xml news feed.Getting “object expected” error on line 88.

Code:

[CODE]

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<script src=”prototype.js” type=”text/javascript”>
<script src=”scriptaculous.js” type=”text/javascript”>
<title>News Feeds</title>
<style type=”text/css”>
.showIt {
font-size: 14pt;
color: green;
font-family: Arial, Tahoma, Verdana;
border: thick solid;
padding: 10px;

}

b { font-size: 16pt;
color:#000000;
}

</style>

<script type=”text/javascript” language=”javascript”>
<!–The object detection code –>
var req = false;
// Is there support for native XHR object?: IE7+, Firefox, Safari, Opera
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
//create an XMLHttpRequest object
}

else if (window.ActiveXObject)
//check for Version 6
{
req = new ActiveXObject(‘MSXML2.XMLHTTP.6.0’);
//create an ActiveX XMLHTTP component
}

if (!req)
{
req = new ActiveXObject(‘MSXML2.XMLHTTP’);
//fallback to version 3
}

function getFeed()
{
if (req)
{
//Request feed data to be retrieved from the server

req.onreadystatechange = function()
{
if (req.readyState == 4 && req.status == 200)
{
var response = req.responseXML;
readXML(response);
}

}
req.open(“GET”, “SportsNewsFeed.xml”, true);
req.send(null);
}
}

function readXML(response)
{
var myResponse = response.documentElement;
var myFeed = myResponse.getElementsByTagName(“channel”);
var place = document.getElementById(“showIt”);
for (var i=0; i < myFeed.length; i++)
{
place.innerHTML += myFeed[i].getElementsByTagName(“title”)[0].firstChild.nodeValue + “:” ;
place.innerHTML += myFeed[i].getElementsByTagName(“link”)[0].firstChild.nodeValue + ‘onmouseover=”Effect.Puff”‘ + “<br><br>”;

}
}

//–>
</script>
</head>

<body onload=”getFeed()”>

<p id=”showIt” class=”showIt”></p>

</body>
</html>

[/CODE]

XML file:

[CODE]
<?xml version=”1.0″ encoding=”utf-8″ ?>
<rss version=”2.0″>

<channel>

<title>Sports News Feed Index</title>
<link>http://sports.espn.go.com/espn/rss/index</link>
<description>Keep up to date on the latest sports news</description>
<language>en</language>

<item>
<title>NFL Headlines</title>
<link>http://sports.espn.go.com/espn/rss/nfl/news</link>
<description>The latest pro football news</description>
</item>

<item>
<title>NBA Headlines</title>
<link>http://sports.espn.go.com/espn/rss/nba/news</link>
<description>The latest pro basketball news</description>
</item>

<item>
<title>MLB Headlines</title>
<link>http://sports.espn.go.com/espn/rss/mlb/news</link>
<description>The latest Major League Baseball news</description>
</item>

<item>
<title>NHL Headlines</title>
<link>http://sports.espn.go.com/espn/rss/nhl/news</link>
<description>The latest pro hockey news</description>
</item>

</channel>

</rss>

[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@criterion9Jul 05.2009 — What line is line 88? Also are you getting anything more helpful from firebug?
Copy linkTweet thisAlerts:
@MichiKenauthorJul 05.2009 — This is Line 88:

[CODE]

<body onload="getFeed()">

[/CODE]
Copy linkTweet thisAlerts:
@MichiKenauthorJul 08.2009 — Ok, got it closer to working. It loops through and displays each of the links, however they show on the HTML page as plain text rather than live url links. I tried putting <a href > </a> tags around the links in the xml file but when it loops through, instead of displaying each of the links, it just says:

NFL Headlines: null

NBA Headlines: null

MLB Headlines: null

NHL Headlines: null

[CODE]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="prototype.js" type="text/javascript"></script>
<script src="scriptaculous.js" type="text/javascript"></script>
<title>News Feeds</title>
<style type="text/css">
.showIt {
font-size: 14pt;
color: green;
font-family: Arial, Tahoma, Verdana;
border: thick solid;
padding: 10px;

}

b { font-size: 16pt;
color:#000000;
}

</style>

<script type="text/javascript" language="javascript">
<!--The object detection code -->
var req = false;
// Is there support for native XHR object?: IE7+, Firefox, Safari, Opera
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
//create an XMLHttpRequest object
}

else if (window.ActiveXObject)
//check for Version 6
{
req = new ActiveXObject('MSXML2.XMLHTTP.6.0');
//create an ActiveX XMLHTTP component
}

if (!req)
{
req = new ActiveXObject('MSXML2.XMLHTTP');
//fallback to version 3
}



function getFeed()
{
if (req)
{
//Request feed data to be retrieved from the server

req.onreadystatechange = function()
{
if (req.readyState == 4 && req.status == 200)
{
var response = req.responseXML;
readXML(response);
}

}
req.open("GET", "SportsNewsFeed.xml", true);
req.send(null);
}
}


function readXML(response)
{
var myResponse = response.documentElement;
var myFeed = myResponse.getElementsByTagName("item");
var place = document.getElementById("showIt");
for (var i=0; i < myFeed.length; i++)
{
place.innerHTML += myFeed[i].getElementsByTagName("title")[0].firstChild.nodeValue + ":" ;
place.innerHTML += myFeed[i].getElementsByTagName("link")[0].firstChild.nodeValue + "<br><br>";

}
}


//-->
</script>
</head>

<body onLoad="getFeed()">


<p id="showIt" class="showIt"></p>



</body>
</html>



[/CODE]


XML file:

[CODE]

<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">

<channel>

<item>
<title>NFL Headlines</title>
<link>http://sports.espn.go.com/espn/rss/nfl/news</link>
<description>The latest pro football news</description>
</item>

<item>
<title>NBA Headlines</title>
<link>http://sports.espn.go.com/espn/rss/nba/news</link>
<description>The latest pro basketball news</description>
</item>

<item>
<title>MLB Headlines</title>
<link>http://sports.espn.go.com/espn/rss/mlb/news</link>
<description>The latest Major League Baseball news</description>
</item>

<item>
<title>NHL Headlines</title>
<link>http://sports.espn.go.com/espn/rss/nhl/news</link>
<description>The latest pro hockey news</description>
</item>


</channel>

</rss>

[/CODE]
×

Success!

Help @MichiKen 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...