ASP RSS Parser, Leughadair Feed
An deireadh sheachdain seo chaidh, tha mi air mo ghleusadh chun laptop agam, a’ coimhead air an lìon airson obair-lìn RSS biadhadh luchd-leughaidh. 'S e an t-adhbhar gun robh mi airson sgrìobhadh ASP Leughadair biadhaidh RSS a sheallas am biadhadh gus an gabhadh an susbaint a sgrìobadh gu fèin-ghluasadach ann an faidhle HTML post-d. Mar sin, dha daoine a tha airson cuibhreann den chuairt-litir post-d aca a ghlèidheadh airson na h-artaigilean Blog no Foillseachaidh aca, dh’ fhaodadh e a bhith air a thoirt a-steach gu furasta.
Leis nach eil JavaScript dha-rìribh a’ taisbeanadh an t-susbaint gus am bi an neach-dèiligidh a’ luchdachadh agus a’ cur an gnìomh an sgriobt, cha robh am pailteas de bhrobhsairean RSS JavaScript feumail. Bha feum agam air leughadair biadhan RSS taobh an fhrithealaiche.
Gus parsadh a XML biadhadh ann an ASP, faodaidh tu an leabharlann MSXML a tha ri fhaighinn ann an ASP a chleachdadh. Seo eisimpleir bunaiteach de mar a nì thu sgrùdadh air inbhir XML a’ cleachdadh ASP:
<%
' Create an instance of the MSXML DOMDocument object
Set xmlDoc = Server.CreateObject("Msxml2.DOMDocument.6.0")
' Load the XML feed from a URL
xmlDoc.async = False
xmlDoc.load("http://example.com/feed.xml")
' Check if the XML is loaded successfully
If xmlDoc.parseError.errorCode <> 0 Then
Response.Write "Error loading XML: " & xmlDoc.parseError.reason
Else
' Navigate through the XML structure and retrieve data
Set items = xmlDoc.selectNodes("//item") ' Change "item" to the appropriate XML element name in your feed
' Loop through the items
For Each item In items
' Access elements within each item
title = item.selectSingleNode("title").text
link = item.selectSingleNode("link").text
description = item.selectSingleNode("description").text
' Perform your sales and marketing operations with the retrieved data
' For example, you can insert this data into a database or display it on a webpage.
Next
End If
' Clean up the XML document
Set xmlDoc = Nothing
%>
Anns a’ chòd seo, bidh sinn an-toiseach a’ cruthachadh eisimpleir den Msxml2.DOMDocument.6.0
nì a bhith ag obair le XML. Bidh sinn an uairsin a’ luchdachadh am biadhadh XML bho a URL agus dèan cinnteach an robh an luchdachadh soirbheachail. Mura h-eil mearachdan ann, cleachdaidh sinn XPath gus seòladh tron structar XML agus dàta fhaighinn air ais bho na h-eileamaidean a shònraicheas tu. Mu dheireadh, faodaidh tu na h-obraichean reic is margaidheachd agad a dhèanamh leis an dàta a chaidh fhaighinn air ais.
Faodaidh tu an toradh atharrachadh gus earrann a thoirt seachad le àireamh shònraichte de dh’ fhaclan agus an uairsin “…” a chuir ris gus innse gu bheil an teacsa a’ leantainn. Seo eisimpleir de mar as urrainn dhut sin a dhèanamh sa chòd ASP agad:
<%
' Create an instance of the MSXML DOMDocument object
Set xmlDoc = Server.CreateObject("Msxml2.DOMDocument.6.0")
' Load the XML feed from a URL
xmlDoc.async = False
xmlDoc.load("http://example.com/feed.xml")
' Check if the XML is loaded successfully
If xmlDoc.parseError.errorCode <> 0 Then
Response.Write "Error loading XML: " & xmlDoc.parseError.reason
Else
' Navigate through the XML structure and retrieve data
Set items = xmlDoc.selectNodes("//item") ' Change "item" to the appropriate XML element name in your feed
' Loop through the items
For Each item In items
' Access elements within each item
title = item.selectSingleNode("title").text
link = item.selectSingleNode("link").text
description = item.selectSingleNode("description").text
' Modify the description to include an excerpt with a specific number of words
excerptLength = 30 ' Change this number to your desired word count
descriptionArray = Split(description, " ")
If UBound(descriptionArray) > excerptLength Then
excerpt = Join(LBound(descriptionArray, excerptLength), " ") & "..."
Else
excerpt = description
End If
' Perform your sales and marketing operations with the excerpt
' For example, you can insert this data into a database or display it on a webpage.
Next
End If
' Clean up the XML document
Set xmlDoc = Nothing
%>
Anns a’ chòd seo, chuir sinn earrann ris a dh’ atharraicheas an description
gus earrann a chruthachadh le àireamh shònraichte de dh’fhaclan (30 san eisimpleir seo) agus cuir “…” ma tha an tuairisgeul nas fhaide. Faodaidh tu an suidheachadh a rèiteachadh excerptLength
caochladair gus smachd a chumail air an àireamh fhaclan anns an earrainn.
Bheir am mion-atharrachadh seo dhut tuairisgeul gearraichte mar earrann airson na h-obraichean reic is margaidheachd agad.
Feuch an cuir thu an àite http://example.com/feed.xml
le URL an inbhir XML tha thu airson ainmean nan eileamaidean agus làimhseachadh dàta a pharsadh agus atharrachadh a rèir an structair agus na riatanasan XML sònraichte agad.