Jump to content

JavaScript RSS/XML data traversal


madsovenielsen

Recommended Posts

HelloI am trying to generate an array of the data in xmlhttp.responseXML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >	<head>		<title>Untitled Page</title>		<script type="text/javascript">			function getFeed()			{				if (window.XMLHttpRequest) {					// code for IE7+, Firefox, Chrome, Opera, Safari					xmlhttp = new XMLHttpRequest();				}				else				{					// code for IE6, IE5					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");				}				xmlhttp.onreadystatechange = function () {					if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {						document.getElementById("text").value = xmlhttp.responseText;						//xmlDoc = xmlhttp.responseXML;						//alert(xmlDoc.length);						//document.getElementById("text").value = xmlDoc.getElementsByTagName("title")[0].nodeValue;					}				}				xmlhttp.open("GET", "http://computers.shop.ebay.com/Computers-Networking-/58058/i.html?rt=nc&_nkw=hp+vectra&_npmv=3&_rss=1", true);				xmlhttp.send();			}	</script>	</head>	<body><h1>eBay RSS feed</h1>		<div id="feed">			<button onclick="getFeed()">Get data</button><br />			<br /><textarea rows="40" cols="150" id="text"></textarea>		</div>	</body></html>

The above code just gets the RSS data from ebay and displays it in the textarea. The parts that have been commented out is where i try to put all the data from xmlhttp.responseXML into an array, i can't seem to make it work.

Webpage error detailsUser Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)Timestamp: Sat, 14 May 2011 18:02:57 UTCMessage: Object requiredLine: 25Char: 25Code: 0URI: file:///C:/Users/user/Desktop/ebayRSSjs.htm

Whats the problem here ?/mads

Link to comment
Share on other sites

I think you are running into the same origin policy. As the link indicates, next generation AJAX will permit this, but there is no AJAX work-around now.What you should be able to do is request the document using your own server as a proxy.
I dont think so. I get the content of response.Text into the textarea just fine. If it was a cross domain issue i would get "Access denied"Theres only a problem when i use responseXML, although the RSS XML validates./mads
Link to comment
Share on other sites

That is the point he is refering to. Only request from the same domain will be reponsed otherwise it will not allow it for security reason. Another work around i can think of now if you are using php as backend server side language you can use CUrl to request to any arbitary remote page and can get the data and then you can format it using DOMdocument and make a output. If you want it to dynamicaly.. make a native php page from the curl response and fetch the data ..then request to that native page (same domain) using ajax and use the response as you need.

Link to comment
Share on other sites

Here is a little snippet of the data i get back from responseText:

<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" xmlns:g="http://base.google.com/ns/1.0" xmlns:rx="urn:ebay:apis:eBLBaseComponents"><!--About this feed===============This page is an RSS feed that provides the listings that satisfy the query you entered.Once you have subscribed to this feed, you will receive automatic updates any time a new listing appears in this store.To learn more about feeds, copy and paste the below link in your browserhttp://pages.ebay.com/help/welcome/contextual/rss.htmlHow to subscribe to this feed=============================You will need an RSS reader. To subscribe copy and Paste the URL of this page into your reader, and follow the instructions provided for that reader.--><channel><title><![CDATA[hp vectra(Category:Computers & Networking)]]></title><cf:treatAs xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005">list</cf:treatAs><link><![CDATA[http://computers.shop.ebay.com/Computers-Networking-/58058/i.html?_nkw=hp+vectra&_npmv=3&ssPageName=RSS:B:SHOP:US:100]]></link><description><![CDATA[hp vectra(Category:Computers & Networking)]]></description><language>en</language><ttl>60</ttl><image><url><![CDATA[http://pics.ebaystatic.com/aw/pics/navbar/eBayLogoTM.gif]]></url><link><![CDATA[http://computers.shop.ebay.com/Computers-Networking-/58058/i.html?_nkw=hp+vectra&_npmv=3&ssPageName=RSS:B:SHOP:US:100]]></link><title><![CDATA[hp vectra(Category:Computers & Networking)]]></title></image><cf:listinfo xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005"><cf:sort ns="urn:ebay:apis:eBLBaseComponents" label="Buy It Now price" element="BuyItNowPrice" data-type="number" default="no"></cf:sort><cf:sort ns="urn:ebay:apis:eBLBaseComponents" label="Current auction price" element="CurrentPrice" data-type="number" default="no"></cf:sort><cf:sort ns="urn:ebay:apis:eBLBaseComponents" label="Listing end time" element="EndTime" data-type="number" default="no"></cf:sort><cf:sort ns="urn:ebay:apis:eBLBaseComponents" label="Number of bids" element="BidCount" data-type="number" default="no"></cf:sort><cf:group ns="urn:ebay:apis:eBLBaseComponents" label="listing format" element="AuctionType"></cf:group><cf:group ns="urn:ebay:apis:eBLBaseComponents" label="option" element="ItemCharacteristic"></cf:group><cf:group ns="urn:ebay:apis:eBLBaseComponents" label="listing category" element="Category"></cf:group></cf:listinfo><item><title><![CDATA[HP Vectra XU90 dual Pentium 133 computer, 160 MB RAM]]></title><link><![CDATA[http://cgi.ebay.com/HP-Vectra-XU90-dual-Pentium-133-computer-160-MB-RAM-/220781305768?pt=Desktop_PCs&hash=item3367975fa8&ssPageName=RSS:B:SHOP:US:101]]></link><description><![CDATA[<table border="0" cellpadding="8"><tr><td width="80px"><a href="http://cgi.ebay.com/HP-Vectra-XU90-dual-Pentium-133-computer-160-MB-RAM-/220781305768?pt=Desktop_PCs&hash=item3367975fa8&ssPageName=RSS:B:SHOP:US:102"><img border="0" src="http://thumbs1.ebaystatic.com/m/mT2Wxds2YLwXWCbFRqM2bVQ/80.jpg"></a></td><td><strong>$9.99</strong> (0 Bids)<br>End Date: Sunday May-15-2011 18:40:47 PDT<br><a href="http://cgi.ebay.com/HP-Vectra-XU90-dual-Pentium-133-computer-160-MB-RAM-/220781305768?pt=Desktop_PCs&hash=item3367975fa8&ssPageName=RSS:B:SHOP:US:103">Bid now</a> | <a href="http://cgi1.ebay.com/ws/eBayISAPI.dll?MfcISAPICommand=MakeTrack&item=220781305768&ssPageName=RSS:B:SHOP:US:104">Add to watch list</a></td></tr></table>]]></description><guid isPermaLink="false"><![CDATA[http://cgi.ebay.com/HP-Vectra-XU90-dual-Pentium-133-computer-160-MB-RAM-/220781305768?pt=Desktop_PCs&hash=item3367975fa8&ssPageName=RSS:B:SHOP:US:101]]></guid><pubDate>Sun, 08 May 2011 18:40:47 PDT</pubDate><rx:CurrentPrice xmlns:rx="urn:ebay:apis:eBLBaseComponents">999</rx:CurrentPrice><rx:EndTime xmlns:rx="urn:ebay:apis:eBLBaseComponents">1305510047000</rx:EndTime><rx:BidCount xmlns:rx="urn:ebay:apis:eBLBaseComponents">0</rx:BidCount><rx:Category xmlns:rx="urn:ebay:apis:eBLBaseComponents"><![CDATA[PC Desktops]]></rx:Category><rx:AuctionType xmlns:rx="urn:ebay:apis:eBLBaseComponents">Auction</rx:AuctionType><rx:ItemCharacteristic xmlns:rx="urn:ebay:apis:eBLBaseComponents">Listed with PayPal</rx:ItemCharacteristic></item><item><title><![CDATA[HP VECTRA XE320 P4 1.7GHz 256MB 20GB CD-ROM Complete]]></title><link><![CDATA[http://cgi.ebay.com/HP-VECTRA-XE320-P4-1-7GHz-256MB-20GB-CD-ROM-Complete-/200376329536?pt=Desktop_PCs&hash=item2ea75c2540&ssPageName=RSS:B:SHOP:US:101]]></link><description><![CDATA[<table border="0" cellpadding="8"><tr><td width="80px"><a href="http://cgi.ebay.com/HP-VECTRA-XE320-P4-1-7GHz-256MB-20GB-CD-ROM-Complete-/200376329536?pt=Desktop_PCs&hash=item2ea75c2540&ssPageName=RSS:B:SHOP:US:102"><img border="0" src="http://thumbs1.ebaystatic.com/m/mHVOiUcsJ8BJjYgA6sIkocA/80.jpg"></a></td><td><strong>$119.00</strong><br>End Date: Tuesday Jun-14-2011 7:24:13 PDT<br>Buy It Now for only: US $119.00<br><a href="http://cgi.ebay.com/HP-VECTRA-XE320-P4-1-7GHz-256MB-20GB-CD-ROM-Complete-/200376329536?pt=Desktop_PCs&hash=item2ea75c2540&ssPageName=RSS:B:SHOP:US:105">Buy it now</a> | <a href="http://cgi1.ebay.com/ws/eBayISAPI.dll?MfcISAPICommand=MakeTrack&item=200376329536&ssPageName=RSS:B:SHOP:US:104">Add to watch list</a></td></tr></table>]]></description><guid isPermaLink="false"><![CDATA[http://cgi.ebay.com/HP-VECTRA-XE320-P4-1-7GHz-256MB-20GB-CD-ROM-Complete-/200376329536?pt=Desktop_PCs&hash=item2ea75c2540&ssPageName=RSS:B:SHOP:US:101]]></guid><pubDate>Sun, 23 Aug 2009 07:19:13 PDT</pubDate><rx:BuyItNowPrice xmlns:rx="urn:ebay:apis:eBLBaseComponents">11900</rx:BuyItNowPrice><rx:CurrentPrice xmlns:rx="urn:ebay:apis:eBLBaseComponents">11900</rx:CurrentPrice><rx:EndTime xmlns:rx="urn:ebay:apis:eBLBaseComponents">1308061453000</rx:EndTime><rx:BidCount xmlns:rx="urn:ebay:apis:eBLBaseComponents">0</rx:BidCount><rx:Category xmlns:rx="urn:ebay:apis:eBLBaseComponents"><![CDATA[PC Desktops]]></rx:Category><rx:AuctionType xmlns:rx="urn:ebay:apis:eBLBaseComponents">Buy it now</rx:AuctionType><rx:ItemCharacteristic xmlns:rx="urn:ebay:apis:eBLBaseComponents">Listed with PayPal</rx:ItemCharacteristic></item><item><title><![CDATA[HP VECTRA XE320 P4 1.7GHz 256MB 20GB CD-ROM Complete]]></title><link><![CDATA[http://cgi.ebay.com/HP-VECTRA-XE320-P4-1-7GHz-256MB-20GB-CD-ROM-Complete-/200382330106?pt=Desktop_PCs&hash=item2ea7b7b4fa&ssPageName=RSS:B:SHOP:US:101]]></link><description><![CDATA[<table border="0" cellpadding="8"><tr><td width="80px"><a href="http://cgi.ebay.com/HP-VECTRA-XE320-P4-1-7GHz-256MB-20GB-CD-ROM-Complete-/200382330106?pt=Desktop_PCs&hash=item2ea7b7b4fa&ssPageName=RSS:B:SHOP:US:102"><img border="0" src="http://thumbs1.ebaystatic.com/m/mHVOiUcsJ8BJjYgA6sIkocA/80.jpg"></a></td><td><strong>$178.95</strong><br>End Date: Wednesday Jun-01-2011 15:05:08 PDT<br>Buy It Now for only: US $178.95<br><a href="http://cgi.ebay.com/HP-VECTRA-XE320-P4-1-7GHz-256MB-20GB-CD-ROM-Complete-/200382330106?pt=Desktop_PCs&hash=item2ea7b7b4fa&ssPageName=RSS:B:SHOP:US:105">Buy it now</a> | <a href="http://cgi1.ebay.com/ws/eBayISAPI.dll?MfcISAPICommand=MakeTrack&item=200382330106&ssPageName=RSS:B:SHOP:US:104">Add to watch list</a></td></tr></table>]]></description><guid isPermaLink="false"><![CDATA[http://cgi.ebay.com/HP-VECTRA-XE320-P4-1-7GHz-256MB-20GB-CD-ROM-Complete-/200382330106?pt=Desktop_PCs&hash=item2ea7b7b4fa&ssPageName=RSS:B:SHOP:US:101]]></guid><pubDate>Wed, 09 Sep 2009 15:00:08 PDT</pubDate><rx:BuyItNowPrice xmlns:rx="urn:ebay:apis:eBLBaseComponents">17895</rx:BuyItNowPrice><rx:CurrentPrice xmlns:rx="urn:ebay:apis:eBLBaseComponents">17895</rx:CurrentPrice><rx:EndTime xmlns:rx="urn:ebay:apis:eBLBaseComponents">1306965908000</rx:EndTime><rx:BidCount xmlns:rx="urn:ebay:apis:eBLBaseComponents">0</rx:BidCount><rx:Category xmlns:rx="urn:ebay:apis:eBLBaseComponents"><![CDATA[PC Desktops]]></rx:Category><rx:AuctionType xmlns:rx="urn:ebay:apis:eBLBaseComponents">Buy it now</rx:AuctionType><rx:ItemCharacteristic xmlns:rx="urn:ebay:apis:eBLBaseComponents">Listed with PayPal</rx:ItemCharacteristic><rx:ItemCharacteristic xmlns:rx="urn:ebay:apis:eBLBaseComponents">Best Offer</rx:ItemCharacteristic></item><item><title><![CDATA[HP VECTRA XE310 P3 1GHz 256MB 20GB CD-ROM Complete Unit]]></title><link><![CDATA[http://cgi.ebay.com/HP-VECTRA-XE310-P3-1GHz-256MB-20GB-CD-ROM-Complete-Unit-/360182515590?pt=Desktop_PCs&hash=item53dc8d0786&ssPageName=RSS:B:SHOP:US:101]]></link><description><![CDATA[<table border="0" cellpadding="8"><tr><td width="80px"><a href="http://cgi.ebay.com/HP-VECTRA-XE310-P3-1GHz-256MB-20GB-CD-ROM-Complete-Unit-/360182515590?pt=Desktop_PCs&hash=item53dc8d0786&ssPageName=RSS:B:SHOP:US:102"><img border="0" src="http://thumbs3.ebaystatic.com/m/mVpJ6uPUfmIgMmMyAtrkPiA/80.jpg"></a></td><td><strong>$99.00</strong><br>End Date: Tuesday Jun-14-2011 7:22:35 PDT<br>Buy It Now for only: US $99.00<br><a href="http://cgi.ebay.com/HP-VECTRA-XE310-P3-1GHz-256MB-20GB-CD-ROM-Complete-Unit-/360182515590?pt=Desktop_PCs&hash=item53dc8d0786&ssPageName=RSS:B:SHOP:US:105">Buy it now</a> | <a href="http://cgi1.ebay.com/ws/eBayISAPI.dll?MfcISAPICommand=MakeTrack&item=360182515590&ssPageName=RSS:B:SHOP:US:104">Add to watch list</a></td></tr></table>]]></description><guid isPermaLink="false"><![CDATA[http://cgi.ebay.com/HP-VECTRA-XE310-P3-1GHz-256MB-20GB-CD-ROM-Complete-Unit-/360182515590?pt=Desktop_PCs&hash=item53dc8d0786&ssPageName=RSS:B:SHOP:US:101]]></guid><pubDate>Sun, 23 Aug 2009 07:17:35 PDT</pubDate><rx:BuyItNowPrice xmlns:rx="urn:ebay:apis:eBLBaseComponents">9900</rx:BuyItNowPrice><rx:CurrentPrice xmlns:rx="urn:ebay:apis:eBLBaseComponents">9900</rx:CurrentPrice><rx:EndTime xmlns:rx="urn:ebay:apis:eBLBaseComponents">1308061355000</rx:EndTime><rx:BidCount xmlns:rx="urn:ebay:apis:eBLBaseComponents">0</rx:BidCount><rx:Category xmlns:rx="urn:ebay:apis:eBLBaseComponents"><![CDATA[PC Desktops]]></rx:Category><rx:AuctionType xmlns:rx="urn:ebay:apis:eBLBaseComponents">Buy it now</rx:AuctionType><rx:ItemCharacteristic xmlns:rx="urn:ebay:apis:eBLBaseComponents">Listed with PayPal</rx:ItemCharacteristic></item>...

If there was a cross domain problem, i would get no data at all./mads

Link to comment
Share on other sites

I did it.This is a fully working example.

<html><head><title></title></head>	<body><h1>eBay RSS feed</h1>		<div id="feed">			<button onclick="getFeed()">Get data</button><br />			<span id="to"></span>		</div>		<script type="text/javascript">						function getFeed() {				if (window.XMLHttpRequest) {				// code for IE7+, Firefox, Chrome, Opera, Safari				xmlhttp = new XMLHttpRequest();				}				else {				// code for IE6, IE5				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");				}				xmlhttp.onreadystatechange = function () {				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {				txt = xmlhttp.responseText;				if (window.DOMParser) {					parser = new DOMParser();					xmlDoc = parser.parseFromString(txt, "text/xml");				}				else // Internet Explorer				{					xmlDoc = new ActiveXObject("Microsoft.XMLDOM");					xmlDoc.async = "false";					xmlDoc.loadXML(txt);				}				document.getElementById("to").innerHTML = xmlDoc.getElementsByTagName("title")[4].childNodes[0].nodeValue;				}			}				xmlhttp.open("GET", "http://computers.shop.ebay.com/Computers-Networking-/58058/i.html?rt=nc&_nkw=hp+vectra&_npmv=3&_rss=1", true);				xmlhttp.send();			}</script>	</body></html>

:)I just needed to turn the data from responseText into a XMLdom object./mads

Link to comment
Share on other sites

That means your browser didn't recognize the XML as correctly formed XML. This often happens with IE. You might try your original code with this line coming before the send method:xmlhttp.overrideMimeType('text/xml');I'm still surprised it works at all. Your original code returns a status of 0 when I try it in Firefox, and I do get an Access Denied error in IE8.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...