Jump to content

xml feed in javascript


Recommended Posts

In order to show this info, you would also need some XSLT to transform it. Because I didn't had much to do, here's a simple XSLT I made few minutes ago specially for you(in other words: I used your feed for tests) :) .

<?xml version="1.0" encoding="windows-1251"?><!-- DWXMLSource="http://www.shareasale.com/dealdatabase2.xml" --><!DOCTYPE xsl:stylesheet  [	<!ENTITY nbsp   " ">	<!ENTITY copy   "©">	<!ENTITY reg    "®">	<!ENTITY trade  "™">	<!ENTITY mdash  "—">	<!ENTITY ldquo  "“">	<!ENTITY rdquo  "”"> 	<!ENTITY pound  "£">	<!ENTITY yen    "¥">	<!ENTITY euro   "€">]><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="iso-8859-1"doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/><xsl:template match="/rss/channel"><xsl:variable name="link" select="//link" /><div><a href="{link}"><xsl:value-of select="title" /></a><br /><xsl:value-of select="description" /><br /><xsl:for-each select="image"><xsl:variable name="url" select="url" /><xsl:variable name="title" select="title" /><xsl:variable name="width" select="width" /><xsl:variable name="height" select="height" /><a href="{link}"><img src="{url}" alt="{title}" width="{width}" height="{height}" /></a><br /></xsl:for-each></div><div><xsl:for-each select="item"><span><a href="{link}"><xsl:value-of select="title" /></a><br /><xsl:value-of select="description" /><br /></span></xsl:for-each></div></xsl:template></xsl:stylesheet>

A recently discovered(well...) JavaScript that would load the feed and the XSLT is this one:

<!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>TEST</title>	<script type="text/javascript">	var sourceXML = 'http://www.shareasale.com/dealdatabase2.xml';	var sourceXSL = 'rss.xsl';	var container = 'xmlData' ;		function getIEXML()	{		var writeObj = document.getElementById(container);				// Load XML		var xml = new ActiveXObject("Microsoft.XMLDOM");		xml.async = false;		xml.load(sourceXML);				// Load the XSL		var xsl = new ActiveXObject("Microsoft.XMLDOM");		xsl.async = false;		xsl.validateOnParse = false;		xsl.load(sourceXSL);				// Transform		writeObj.innerHTML = xml.transformNode(xsl);	}		function startMozXML()	{		//Load the XML		xmlhttpXSL = new XMLHttpRequest();		xmlhttpXSL.open("GET", sourceXSL, false);		xmlhttpXSL.setRequestHeader("Content-Type", "text/css")		xmlhttpXSL.send(null);		loadedStyle =  xmlhttpXSL.responseXML;				//Load the XSL		xmlhttpXML = new XMLHttpRequest();		xmlhttpXML.open("GET", sourceXML, false);		xmlhttpXML.setRequestHeader("Content-Type", "text/xml")		xmlhttpXML.send(null);		var xmlDoc = xmlhttpXML.responseXML;				//Transform		var writeObj = document.getElementById(container);		var xsltProcessor = new XSLTProcessor();		var xmls = new XMLSerializer();		xsltProcessor.importStylesheet(loadedStyle);		var xmlDoc = xsltProcessor.transformToDocument(xmlhttpXML.responseXML);		writeObj.innerHTML = xmls.serializeToString(xmlDoc);	}		function init()	{	  if(window.ActiveXObject)		{		getIEXML();		}		else			if(window.XMLHttpRequest && window.XSLTProcessor)				{				startMozXML();				}			else				{				document.getElementById(container). innerHTML = 'No Support In This Browser..'				}	}	window.onload = init;	</script></head><body><div id="xmlData">In order to see this, you must turn on your JavaScript.<br />If yo have turned on your JavaScript, refresh the page and wait.</div></body></html>

However, now that you mentioned it, I tested the above code and... khm... the only way it will run is if the XML and the XSLT are in one folder. It can't work with absolute paths.Perhaps if you or anyone else could adjust the code for absolute paths, this code will get all feeds out there(or atleast this one :) ).

Link to comment
Share on other sites

The problem is security, crossing domains is not allowed( depending on the browser ).@boen_robot, your code works good in IE even across the domains.When used in FF it gives the error "uncaught exception: Permission denied".See "Security Issues" in this article:xmlhttpreq.html@davon, Are you using something server side that can retreive and store the xml file.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...