go*********@gma il.com wrote:
I am using MSXML 4.0
I am grabbing numerous RSS News Feeds and trying to parse the data and
read it into a dB. The problem is that the RSS News feeds are not all
the same when tunneled down in the item element.
They ALL contain the nodes: title, description, link and pubDate. But
they come in all different orders and have other child nodes sandwiched
among the ones I am after.
<channel>
<item>
<title>
<description>
<link>
<pubDate>
example: http://rss.news.yahoo.com/rss/elections
example: http://www.cbsnews.com/feeds/rss/main.rss
example: http://rss.cnn.com/rss/si_topstories.rss
My code issue is;
When looping through the recordset I have to reference the childnode
numerically ie. item.childnodes .item(0).text,
item.childnodes .item(1).text, item.childnodes .item(2).text
You need to learn XPath and use selectNodes/selectSingleNod e to find the
right elements and their content, the following shows how to do that
with VBScript in ASP using MSXML 4 and selectNodes, selectSingleNod e. It
is also possible to solve that without XPath using getElementsByTa gName
as needed.
Watch out for line breaks introduced by the post but not allowed in
vbscript
:
<%@ Language="VBScr ipt" %>
<html lang="en">
<head>
<title>Some feeds</title>
<script runat="server" language="VBScr ipt">
Sub OutputFeed (feedURL)
Dim XmlDocument
Set XmlDocument = Server.CreateOb ject("Msxml2.DO MDocument.4.0")
XmlDocument.set Property "ServerHTTPRequ est", true
XmlDocument.asy nc = False
Dim Loaded
Loaded = XmlDocument.loa d(feedURL)
If Loaded Then
Dim Items, Item, Title, Link, Description, TitleText,
DescriptionText , LinkURL
Set Items = XmlDocument.sel ectNodes("//item")
Response.Write "<p>Feed from " & feedURL & " has currently " &
Items.Length & " items.</p>" & VbCrLf
Response.Write "<ul>" & VbCrLf
For Each Item in Items
Set Title = Item.selectSing leNode("title/text()")
If Title Is Nothing Then
TitleText = ""
Else
TitleText = Title.data
End If
Set Link = Item.selectSing leNode("link/text()")
If Link Is Nothing Then
LinkURL = ""
Else
LinkURL = Trim(Link.data)
End If
Set Description = Item.selectSing leNode("descrip tion/text()")
If Description Is Nothing Then
DescriptionText = ""
Else
DescriptionText = Description.dat a
End If
Response.Write "<li><a href=""" & LinkURL & """>" & TitleText &
": " & DescriptionText & "</a></li>" & VbCrLf
Next
Response.Write "</ul>" & VbCrLf
End If
End Sub
</script>
</head>
<body>
<h1>Some feeds</h1>
<%
OutputFeed("htt p://rss.news.yahoo. com/rss/elections")
OutputFeed("htt p://www.cbsnews.com/feeds/rss/main.rss")
OutputFeed("htt p://rss.cnn.com/rss/si_topstories.r ss")
%>
</body>
</html>
--
Martin Honnen
http://JavaScript.FAQTs.com/