Tutorial per gestire feed rss e atom
Per creare e leggere feed RSS ed atom prendendo i dati dal vostro DB.
script asp per generare il file xml per feed RSS
per cominciare dobbiamo creare la funzione per gestire il formato della data che poi andremo ad includere nel nostro file che genera il feed
dataRFC-822.asp
<%
Function return_RFC822_Date(myDate, offset)
Dim myDay, myDays, myMonth, myYear
Dim myHours, myMonths, mySeconds
myDate = CDate(myDate)
myDay = WeekdayName(Weekday(myDate),true)
myDays = Day(myDate)
myMonth = MonthName(Month(myDate), true)
myYear = Year(myDate)
myHours = zeroPad(Hour(myDate), 2)
myMinutes = zeroPad(Minute(myDate), 2)
mySeconds = zeroPad(Second(myDate), 2)
return_RFC822_Date = myDay&", "& _
myDays&" "& _
myMonth&" "& _
myYear&" "& _
myHours&":"& _
myMinutes&":"& _
mySeconds&" "& _
offset
End Function
Function zeroPad(m, t)
zeroPad = String(t-Len(m),"0")&m
End Function
%>
script per generare il feed ovviamente dovete cambiare le parti che riguardano le chiamate di collegamento e la query al vostro database
<%
Session.LCID = 1033
%>
<!-- #include virtual='/public/aspnuke/dataRFC-822.asp' -->
<%
dim Conn, SQL, rs
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " &
Server.MapPath("/public/aspnuke/db/main.mdb")
Set rs = Server.CreateObject("ADODB.RecordSet")
'Istruzione SQL che prende le news dal Data Base; modificate il nome della
vostra tabella e i vari nomi dei campi
'SQL ="SELECT TOP 1 nID, nTitle, nHeader, nText, nValid FROM news WHERE nValid=true
Order by nID desc"
SQL ="SELECT TOP 10 nID, nTitle, nAuthor, nDate, nHeader, nText, nImage, nURL,
nRead, nCategory, nValid FROM news WHERE nValid=true ORDER by nDate desc"
rs.Open SQL, conn,3,3
' Numero totale di records
totfiles = rs.recordcount
xml = "<?xml version=""1.0"" encoding=""UTF-8""?><rss version=""0.91""><channel><copyright>Copyright
2004 Pillole.org</copyright><title>Le Ultime Pillole</title><description>Pillole
di medicina telematica </description><link>http://www.pillole.org</link><webMaster>info@pillole.org</webMaster><language>it-it</language>"
xml= xml &"<image><title>Pillole.org</title><url>http://www.pillole.org/cornice_file/logopillole.jpg</url><link>http://www.pillole.org</link><width>74</width><height>72</height><description>Pillole
di medicina telematica</description></image>"
'Se il Record Set non è vuoto
if totfiles <>0 then
dim titolo, testata, datapil
Do while not rs.eof
titolo= rs("nTitle")
datapil= mid(rs("ndate"),7,2)&"/"&mid(rs("ndate"),5,2)&"/"&mid(rs("ndate"),1,4)
datapil= return_RFC822_Date(datapil, "+0100")
titolo= replace(titolo, "è",CHR(232))
titolo= replace(titolo, "à",CHR(224))
'titolo = datapil &" - " & titolo
testata= rs("nHeader")
testata= replace(testata, "à",CHR(224))
testata= replace(testata, "è",CHR(232))
xml = xml & "<item>"
xml = xml & "<pubDate><![CDATA[" & Server.HTMLEncode(datapil) & "]]></pubDate>"
xml = xml & "<title><![CDATA[" & Server.HTMLEncode(titolo) & "]]></title>"
xml = xml & "<description><![CDATA[" & Server.HTMLEncode(testata) & "]]></description>"
xml = xml & "<link><![CDATA[http://www.pillole.org/public/aspnuke/news.asp?id="
& rs("nID") & "&sid="&session.sessionid & "]]></link>"
xml = xml & "</item>"
rs.movenext
Loop
End if
xml = xml & "</channel></rss>"
' Impostazione che setta il tipo di file in output su XML
response.ContentType = "text/xml"
response.write (xml)
'Libero Risorse
rs.close
set rs=nothing
Conn.Close
set Conn=nothing
%>
files per leggere il feed RSS
<%
Option Explicit
Response.Buffer = true
Response.ExpiresAbsolute = Now () - 1
Response.Expires = 0
%>
<%
Session.LCID=1040
Dim url, objXML, blnLoaded, objNodeList, objNode, objNode2, html, strTitle,
strDescription, strURL, strPubDate
' Script ASP che rielabora un documento XML RSS e RDF e fornisce in output un
documento HTML
url="http://www.pillole.org/public/aspnuke/feednews.asp"
' Creo oggetto DOM XML
Set objXML = Server.CreateObject("msxml2.DOMDocument.3.0")
objXML.async = false
objXML.setProperty "ServerHTTPRequest", True
' validazione del documento XML
objXML.validateOnParse =false' true
' non conservare spazi
objXML.preserveWhiteSpace = false
blnLoaded = objXML.Load(url)
If Not blnLoaded Then
Response.write "<div align=""center"" style=""font-size: 10px; font-family:
Verdana"" Nessuna news da visualizzare</div>"
Else
set objNodeList = objXML.getElementsByTagName("channel")
For Each objNode In objNodeList
For Each objNode2 In objNode.childNodes
Select Case objNode2.nodeName
Case "pubDate"
html = html + "<tr><td style=""font-size: 10px; font-family: Verdana"" ><b>"
html = html + objNode2.firstChild.nodevalue
html = html + "</b></td></tr>"
Case "title"
html = html + "<tr><td style=""font-size: 10px; font-family: Verdana"" ><b>"
html = html + objNode2.firstChild.nodevalue
html = html + "</b></td></tr>"
Case "link"
html = html + "<tr><td style=""font-size: 10px; font-family: Verdana"" ><a
target=_blank href=" + objNode2.firstChild.nodevalue + ">"
html = html + objNode2.firstChild.nodevalue
html = html + "</a></td></tr>"
Case "description"
html = html + "<tr><td style=""font-size: 10px; font-family: Verdana""><i>"
html = html + objNode2.firstChild.nodevalue
html = html + "</i></td></tr>"
End Select
Next
Next
html = html + "<tr><td style=""font-size: 10px; font-family: Verdana""><hr></td></tr>"
Set objNodeList = objXML.getElementsByTagName("item")
For Each objNode In objNodeList
For Each objNode2 In objNode.childNodes
Select Case objNode2.nodeName
Case "pubDate"
strpubDate = objNode2.firstChild.nodevalue
Case "title"
strTitle = objNode2.firstChild.nodevalue
Case "link"
strURL = objNode2.firstChild.nodevalue
Case "description"
strDescription = objNode2.firstChild.nodevalue
End Select
Next
html = html + "<tr><td style=""font-size: 10px; font-family: Verdana"" align=""justify""><li/><b>"
+ strpubDate + " - <a target=_blank href=" + strURL + ">" + strTitle +
"</a></b><br>" + strDescription +"</td></tr>"
strTitle = ""
strURL = ""
strDescription = ""
strpubDate = ""
Next
html = "<table width=""400"">"+html+"</table>"
set objNodeList = Nothing
End if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<style TYPE="text/css">
A {color:blue; text-decoration: none;}
A:hover{color:red; text-decoration:underline; }
</style>
</head>
<body style="font-size: 10px; font-family: Verdana" align="justify">
<%
dim effetto_marquee,elenco_comunicati,scrollSpeed,scrollTop,scrollLeft,scrollWidth,scrollHeight
effetto_marquee=""
scrollSpeed = 85
scrollTop = 0
scrollLeft = 0
scrollWidth = "100%"
scrollHeight = 72
effetto_marquee="<MARQUEE onmouseover='this.stop();' " & _
"onmouseout='this.start();'direction='up' scrollamount='1' " & _
"scrolldelay='" & scrollSpeed & "' Top='" & scrollTop & "' left='" & scrollLeft
& _
"' width='" & scrollWidth & "' height='" & scrollHeight & "'>" & html & "</MARQUEE>"
Response.Write(effetto_Marquee)
%>
</body>
</html>
<%
Option Explicit
Response.Buffer = true
Response.ExpiresAbsolute = Now () - 1
Response.Expires = 0
%>
<%
Session.LCID=1040
Dim url, objXML, blnLoaded, objNodeList, objNode, objNode2, html, strTitle,
strDescription, strURL, strpubDate
' Script ASP che rielabora un documento XML RSS e RDF e fornisce in output un
documento HTML
url="http://www.pillole.org/public/aspnuke/feednewscardio.asp"
' Creo oggetto DOM XML
Set objXML = Server.CreateObject("msxml2.DOMDocument.3.0")
objXML.async = false
objXML.setProperty "ServerHTTPRequest", True
' validazione del documento XML
objXML.validateOnParse =false' true
' non conservare spazi
objXML.preserveWhiteSpace = false
blnLoaded = objXML.Load(url)
If Not blnLoaded Then
Response.write "<div align=""center"" style=""font-size: 10px; font-family:
Verdana"" Nessuna news da visualizzare</div>"
Else
set objNodeList = objXML.getElementsByTagName("channel")
For Each objNode In objNodeList
For Each objNode2 In objNode.childNodes
Select Case objNode2.nodeName
Case "title"
html = html + "<b>"
html = html + objNode2.firstChild.nodevalue
html = html + " | </b>"
Case "pubDate"
html = html + "<b>"
html = html + objNode2.firstChild.nodevalue
html = html + " - </b>"
Case "link"
html = html + "<a target=_blank href=" + objNode2.firstChild.nodevalue + ">"
html = html + objNode2.firstChild.nodevalue
html = html + "</a> | "
Case "description"
html = html + "<i>"
html = html + objNode2.firstChild.nodevalue
html = html + "</i> | " & Date() &" | "
End Select
Next
Next
Set objNodeList = objXML.getElementsByTagName("item")
For Each objNode In objNodeList
For Each objNode2 In objNode.childNodes
Select Case objNode2.nodeName
Case "title"
strTitle = objNode2.firstChild.nodevalue
Case "pubDate"
html = html + "<b>"
html = html + objNode2.firstChild.nodevalue
html = html + " - </b>"
Case "link"
strURL = objNode2.firstChild.nodevalue
Case "description"
strDescription = objNode2.firstChild.nodevalue
End Select
Next
html = html + "<b> <a target=_blank href=" + strURL + ">" + strTitle + "</a> |
</b>"
strTitle = ""
strURL = ""
strDescription = ""
strpubDate = ""
Next
set objNodeList = Nothing
End if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<style TYPE="text/css">
A {color:blue; text-decoration: none;}
A:hover{color:red; text-decoration:underline; }
</style>
</head>
<body style="font-size: 10px; font-family: Verdana" align="justify">
<%
dim effetto_marquee,scrollSpeed,scrollTop,scrollLeft,scrollWidth,scrollHeight
effetto_marquee=""
scrollSpeed = 1
scrollTop = 0
scrollLeft = 0
scrollWidth = 300
scrollHeight = 24
effetto_marquee="<MARQUEE onmouseover='this.stop();' " & _
"onmouseout='this.start();'direction='left' scrollamount='1' " & _
"scrolldelay='" & scrollSpeed & "' Top='" & scrollTop & "' left='" & scrollLeft
& _
"' width='" & scrollWidth & "' height='" & scrollHeight & "'>" & html & "</MARQUEE>"
Response.Write(effetto_Marquee)
%>
</body>
</html>
esempi per atom
file per gestire la data
dataperatom.asp
<%
Function return_RFC822_Date(myDate, offset)
Dim myDay, myDays, myMonth, myYear
Dim myHours, myMonths, mySeconds
myDate = CDate(myDate)
myDay = WeekdayName(Weekday(myDate),true)
myDays = Day(myDate)
If Len(myDays) = 1 Then
myDays = "0" & myDays
end if
myMonth=Month(myDate)
If Len(myMonth) = 1 Then
myMonth = "0" & myMonth
end if
myYear = Year(myDate)
myHours = zeroPad(Hour(myDate), 2)
myMinutes = zeroPad(Minute(myDate), 2)
mySeconds = zeroPad(Second(myDate), 2)
return_RFC822_Date = myYear&"-"& _
myMonth&"-"& _
myDays&"T"& _
myHours&":"& _
myMinutes&":"& _
mySeconds&""& _
offset
End Function
Function zeroPad(m, t)
zeroPad = String(t-Len(m),"0")&m
End Function
%>
file per generare xml per atom
Session.LCID = 1033
%>
<!-- #include virtual='/public/aspnuke/dataperatom.asp' -->
<%
dim Conn, SQL, rs, xml
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " &
Server.MapPath("/public/aspnuke/db/main.mdb")
Set rs = Server.CreateObject("ADODB.RecordSet")
'Istruzione SQL che prende le news dal Data Base; modificate il nome della
vostra tabella e i vari nomi dei campi
'SQL ="SELECT TOP 1 nID, nTitle, nHeader, nText, nValid FROM news WHERE nValid=true
Order by nID desc"
SQL ="SELECT TOP 10 nID, nTitle, nAuthor, nDate, nHeader, nText, nImage, nURL,
nRead, nCategory, nValid FROM news WHERE nValid=true ORDER by nDate desc"
rs.Open SQL, conn,3,3
' Numero totale di records
totfiles = rs.recordcount
'xml = "<?xml version=""1.0"" encoding=""iso-8859-1""?>"
xml = "<?xml version=""1.0"" encoding=""UTF-8""?>"
xml=xml &"<feed version=""0.3"" xmlns:wfw=""http://wellformedweb.org/CommentAPI/""
xmlns:slash=""http://purl.org/rss/1.0/modules/slash/"" xmlns:trackback=""http://madskills.com/public/xml/rss/module/trackback/""
xmlns=""http://purl.org/atom/ns#"" xml:lang=""it-it"">"
xml=xml &"<title>Pillole.org</title><link rel=""alternate"" type=""text/html""
href=""http://www.pillole.org"" />"
xml=xml&"<tagline type=""text/html"">Pillole.org</tagline><id>http://www.pillole.org</id>"
xml=xml&"<generator url=""http://www.pillole.org/public/aspnuke/feedatom.asp""
version=""Pillole.org"">feed.Pillole.org 0.11</generator>"
xml=xml&"<author><name>Pillole.org</name><url>http://www.pillole.org</url></author><modified>"
& return_RFC822_Date(Date(),"+01:00") & "</modified>"
'Se il Record Set non è vuoto
if totfiles <>0 then
dim titolo, testata, datapil
Do while not rs.eof
titolo= rs("nTitle")
datapil= mid(rs("ndate"),7,2)&"/"&mid(rs("ndate"),5,2)&"/"&mid(rs("ndate"),1,4)
datapil= return_RFC822_Date(datapil,"+01:00")
titolo= replace(titolo, "è",CHR(232))
titolo= replace(titolo, "é",CHR(233))
titolo= replace(titolo, "à",CHR(224))
titolo= replace(titolo, "ì",CHR(236))
titolo= replace(titolo, "ò",CHR(242))
titolo= replace(titolo, "ù",CHR(249))
'titolo = datapil &" - " & titolo
testata= rs("nHeader")
testata= replace(testata, "è",CHR(232))
testata= replace(testata, "é",CHR(233))
testata= replace(testata, "à",CHR(224))
testata= replace(testata, "ì",CHR(236))
testata= replace(testata, "ò",CHR(242))
testata= replace(testata, "ù",CHR(249))
xml = xml & "<entry>"
xml = xml & "<title type=""text/html"" mode=""escaped"">" & Server.HTMLEncode(titolo)
& "</title>"
xml = xml & "<id>http://www.pillole.org/public/aspnuke/news.asp?id=" & rs("nID")
& "&sid=" & session.sessionid & "</id>"
xml = xml & "<created>" & Server.HTMLEncode(datapil) & "</created>"
xml = xml & "<content type=""text/html"" mode=""escaped"">" & Server.HTMLEncode(testata)
& "</content>"
xml = xml & "<link rel=""alternate"" type=""text/html"" href=""http://www.pillole.org/public/aspnuke/news.asp?id="
& rs("nID") & "&sid=" & session.sessionid & """/>"
xml = xml & "<issued>" & Server.HTMLEncode(datapil) & "</issued>"
xml = xml & "<modified>" & Server.HTMLEncode(datapil) & "</modified>"
xml = xml & "<wfw:comments>http://www.pillole.org/public/aspnuke/comments.asp?id="
& rs("nID") & "&sid=" & session.sessionid & "</wfw:comments>"
xml = xml & "</entry>"
rs.movenext
Loop
End if
xml = xml & "</feed>"
' Impostazione che setta il tipo di file in output su XML
response.ContentType = "text/xml"
response.write (xml)
'Libero Risorse
rs.close
set rs=nothing
Conn.Close
set Conn=nothing
%>
legge feed atom esempio 1
<%
Sub atom(URL)
Set objXML = Server.CreateObject("msxml2.DOMDocument.3.0")
objXML.async = false
objXML.setProperty "ServerHTTPRequest", True
objXML.validateOnParse = true
objXML.preserveWhiteSpace = false
If Not objXML.Load(URL) Then
Response.write "<P>ERROR<br>code: " & _
objXML.parseError.errorCode & _
"<br>Linea/Col: " & _
objXML.parseError.line & "/" & _
objXML.parseError.linepos & "</P>"
Else
Set objNodeList = objXML.getElementsByTagName("entry")
For Each objNode In objNodeList
For Each objNode2 In objNode.childNodes
Select Case objNode2.nodeName
Case "issued"
strdate= left(objNode2.firstChild.nodevalue,10)
Case "link"
strURL = objNode2.GetAttribute("href")
Case "title"
strTitle = objNode2.firstChild.nodevalue
Case "content"
strDescription = objNode2.firstChild.data
For Each objNode3 In objNode2.childNodes
Select Case objNode3.nodeName
Case "div"
StrDescription = objNode3.text
For Each objNode4 In objNode3.childNodes
Select Case objNode4.nodeName
Case "a"
linkable=objNode4.firstChild.text
htmlink=" <a href='"& _
objNode4.GetAttribute("href")&"'>"& _
linkable&"</a>"
StrDescription = replace(StrDescription,linkable,htmlink)
end select
next
end select
next
End Select
Next
if strdate<>prevstrdate then
response.write "</ul><b>"&strdate&"</b><ul>"
prevstrdate=strdate
end if
Response.write "<li><b><a href=""" & strURL & """>" & _
strTitle & "</a></b><br>" & strDescription
strTitle = ""
strURL = ""
strDescription = ""
Next
set objNodeList = Nothing
End if
End sub
'call atom("http://newsgroup.aspitalia.com/feed/atom.xml")
call atom("http://www.pillole.org/public/aspnuke/feedatom.asp")
%>
legge feed atom esempio 2
<%
' Fetch an Atom XML file by HTTP and return an array of dictionaries containing
article details
Function atom(strURL)
Set objXML = Server.CreateObject("msxml2.DOMDocument.3.0")
objXML.async = false
objXML.setProperty "ServerHTTPRequest", True
objXML.validateOnParse = true
objXML.preserveWhiteSpace = false
If Not objXML.Load(strURL) Then
Response.write "<P>ERROR<br>code: " & _
objXML.parseError.errorCode & _
"<br>Linea/Col: " & _
objXML.parseError.line & "/" & _
objXML.parseError.linepos & "</P>"
' Return an empty array
atom = array()
Else
Dim items()
' Get the entries
Set objNodeList = objXML.getElementsByTagName("entry")
' Create the return list
numEntries = objNodeList.length
ReDim items( numEntries )
itemCount = 0
For Each objNode In objNodeList
itemCount = itemCount + 1
set items(itemCount) = Server.CreateObject("Scripting.Dictionary")
For Each objNode2 In objNode.childNodes
Select Case objNode2.nodeName
Case "issued"
items(itemCount).Add "issued", left(objNode2.firstChild.nodevalue,10)
Case "link"
If objNode2.GetAttribute("rel") <> "service.edit" Then
items(itemCount).Add "link", objNode2.GetAttribute("href")
End If
Case "title"
items(itemCount).Add "title", objNode2.firstChild.nodevalue
Case "id"
items(itemCount).Add "id", objNode2.firstChild.nodevalue
Case "summary"
items(itemCount).Add "summary", objNode2.firstChild.nodevalue
Case "content"
strDescription = ""
If objNode2.hasChildNodes Then
For Each subNode In objNode2.childNodes
strDescription = strDescription & subNode.xml
Next
Else
strDescription = objNode2.text
End If
items(itemCount).Add "content", strDescription
end select
Next
Next
set objNodeList = Nothing
atom = items
set objXML = nothing
End If
End Function
' And now the example. Display the full first entry, then clickable links to the
rest.
'blogURL = "http://www.iteam5.net/blog/feed.xml"
blogURL = "http://www.pillole.org/public/aspnuke/feedatom.asp"
blogItems = atom(blogURL)
%>
<html>
<head>
<title>Notizie Pillole</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<style TYPE="text/css">
A {color:blue; text-decoration: none;}
A:hover{color:red; text-decoration:underline; }
</style>
</head>
<body style="font-size: 10px; font-family: Verdana" align="justify">
<table border="0" width="500">
<tr><td style="font-size: 10px; font-family: Verdana" align="justify">
<%
If UBound(blogItems) > 0 Then
response.write "<h5><a href=""" & blogItems(1).Item("id") & """>"& blogItems(1).Item("title")
& "</a></h5>" & vbCrLf
response.write "<h5>" & blogItems(1).Item("content") & "</h5>"
End If
If UBound(blogItems) > 1 Then
response.write "<h5>Articoli recenti</h5>" & vbCrLf & "<ul>"
For i = 2 to UBound(blogItems)
response.write " <li><h6><a href=""" & blogItems(i).Item("id") & """>" & _
blogItems(i).Item("title") & "</a></h6></li>" & vbCrLf
Next
response.Write "</ul>"
End If
%>
</td></tr></table>
</body>
</html>
legge feed atom con scroller verticale e javascript
modificato lo script di Francesco Passantino
Email: francesco@iteam5.net