Jump to content

Extracting data from XML news feed


Wolverine68

Recommended Posts

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

 

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>


 

XML file:

 

 


<?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>

Link to comment
Share on other sites

Ok, got it closer to working. It loops through and displays each of the links. However, the links show on the HTML page as plain text rather than live url links.

 


<!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>


 

XML file:

 


<?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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.