Show RSS by screen

0

I am learning to work with RSS and the truth is that I am very lost, I have this code but on the screen I only get "Loading Rss ..." but never upload anything. What is this about? What am I doing wrong?

<!DOCTYPE html>
<html>  
<head>
<meta charset="utf-8">
<title>Ejemplo basico AJAX y XML</title>   
<script type="text/javascript">
function cargar(){

	var objHttp=null;
	if(window.XMLHttpRequest) {
		objHttp = new XMLHttpRequest(); 
	} else if(window.ActiveXObject) {
		objHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	}	
  
  objHttp.open("GET", "http://www.heraldo.es/index.php/mod.portadas/mem.rss" , true);  
  
  objHttp.onreadystatechange = function() {
	 if (objHttp.readyState == 4 && objHttp.status == 200) {
	 	console.log(objHttp.responseXML);
		var fichero = objHttp.responseXML; //recogemos el contenido 
		var noticias = fichero.documentElement;
		//var noticias = fichero.getElementsByTagName("item");
		var cadena = "";	
		for (i = 0;i < 3; i++){ 			
			cadena = cadena + "Titular: " + noticias.getElementsByTagName("item")[i].childNodes[1].firstChild.nodeValue + "<br/>";						
			cadena = cadena + "Descripcion: " + noticias.getElementsByTagName("item")[i].childNodes[9].firstChild.nodeValue + "<br/>";
			cadena = cadena + "enlace <a href='" + noticias.getElementsByTagName("item")[i].childNodes[7].firstChild.nodeValue + "'> Link </a><br/><br/><br/>";
			//cadena = cadena + "enlace "+ noticias.getElementsByTagName("link")[i].childNodes[0].firstChild.nodeValue + "<br/><br/><br/>";
		}
		alert(cadena);
		document.getElementById("caja").innerHTML = cadena;
	 } 
	}
	objHttp.send(null);            
}

</script>
</head>   
	<body >  	
		<div style="width:400px;height:400px; position:relative;" id="caja">	
			Cargando Rss...
		</div>	
		<script>cargar();</script>
		</div>
	</body>   
</html>

Cheers!

    
asked by NEA 13.02.2018 в 19:02
source

1 answer

0

Since it has its proper complexity to read an RSS, I can recommend a JS library.

To read RSS (XML) you can use FeedEk is a library that will allow you to read almost any RSS and also choose what things to show.

Here is a demo: link

Try the URL of the RSS you want to read: link

The code you would write would be:

  $('#divRss').FeedEk({
    FeedUrl: 'http://www.heraldo.es/index.php/mod.portadas/mem.rss',
    MaxCount: 3,
    ShowDesc: true,
    ShowPubDate: false,
    DescCharacterLimit: 100
  });

And you would see this in the browser:

With a bit of calm you can adapt it to the style of your website. Or simply show the links to the entry (news or announcement, as you want to understand it) of the site that issues it.

    
answered by 13.02.2018 в 19:50