pubblicato 12.07.06 10:47
Nell'esempio seguente vedremo come leggere un feed RSS 2.0 con ASP e stampare nella pagina i titoli di ogni news contenuta con la data corrispondente.
Lo script si appoggia all'oggetto MSXML2.Domdocument, disponibile di default in una configurazione base di ASP 3.0.
Come prima cosa creiamo una variabile specificando l'URL del feed da leggere.
dim strError, strURL strURL = "http://www.simonecarletti.com/blog/index.xml"
Ora inizializziamo il parser.
E' importante verificare che il feed sia valido poiché il parser non esegue alcun controllo sulla corrispondenza al formato RSS 2.0.
dim xmlDom, nodeCol, oNode, oChildNode
set xmlDom = Server.CreateObject("MSXML2.Domdocument")
xmlDOM.async = False
call xmlDom.setProperty("ServerHTTPRequest", true)
xmlDom.async = false
call xmlDom.load(strURL)
A questo punto abbiamo il contenuto del feed. Non resta che analizzarlo e mostrare a video quanto ci interessa. Nell'esempio seguente verrà stampato a video il contenuto del feed e per ogni item sarà disponibile la data di pubblicazione ed il titolo, con un collegamento al post.
if not xmlDom.documentElement is nothing then
set nodeCol = xmlDom.documentElement.selectNodes("channel/item")
ii = 1
for each objNode in nodeCol
Response.Write("<div>" & vbCrLf)
set oChildNode = objNode.selectSingleNode("pubDate")
if not oChildNode is nothing then
strPubDate = Server.HTMLEncode(oChildNode.text)
strPubDate = replace(strPubDate, "'", "'")
strPubDate = replace(strPubDate, "&", "&")
strPubDate = replace(strPubDate, vbCrLf, "<br />")
Response.Write("<strong>" & strPubDate & "</strong><br />" & vbCrLf)
end if
set oChildNode = objNode.selectSingleNode("link")
if not oChildNode is nothing then
Response.Write(" <a href='" & oChildNode.text & "'>")
strLink = "yes"
end if
set oChildNode = objNode.selectSingleNode("title")
if not oChildNode is nothing then
strTitle = Server.HTMLEncode(oChildNode.text)
strTitle = replace(strTitle, "'", "'")
strTitle = replace(strTitle, "&", "&")
strTitle = replace(strTitle, vbCrLf, "<br />")
Response.Write("" & strTitle & "")
end if
if strLink = "yes" then
Response.Write("</a><br /><br />" & vbCrLf)
end if
'set oChildNode = objNode.selectSingleNode("description")
'if not oChildNode is nothing then
' Response.Write(" " & objChildNode.text & "<br />" & vbCrLf)
'end if
'if i > 7 then exit for
ii = ii + 1
Response.Write("</div>" & vbCrLf)
next
else
Response.Write(strError & vbCrLf)
end if
Il risultato è mostrato nell'immagine seguente.

E' disponibile il codice completo in formato .txt.
TrackBack
TrackBack URL for this entry:
http://www.rss-world.info/cgi-bin/mt/mt-rsstb.cgi/594


