Accessing XML elements by name with ASP

G

gordon.lear

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

But because I am trying to use the same code for every feed I want to
access the childnodes by their names: item.childnodes.item(title).text,
item.childnodes.item(description).text, item.childnodes.item(link).text
because in some feeds 0=title and in others 0=link etc etc...

I have spent three days searching the net and have not found anything.

Does anyone have a way to do this?

For those that want to give me their .Net solution I am sad to say it
must be done using ASP/ADO.

Please show me the way!!

||

-Gordon

glearATgmail.com
 
G

gmlear

My bad. Don't use glearATgmail.com it is old. Just reply to this
post. I have it starred. thx!!
 
W

William Park

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> ....
For those that want to give me their .Net solution I am sad to say it
must be done using ASP/ADO.


I'm not sure how you would translate this to ASP, but this is how I
would do using Expat parser and Bash shell:

data () # Usage: data data
{
tmp=$1
set -- ${XML_ELEMENT_STACK[*]|=+([a-zA-Z])}
if [[ $2.$3 == item.channel ]]; then
case $1 in
title|description|link|pubDate) strcpy $1 $tmp ;;
esac
fi
}
end () # Usage: end tag
{
[[ $1 == item ]] && declare -p title description link pubDate
}
xml -d data -e end '<channel> ... </channel>'
 
M

Martin Honnen

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/selectSingleNode to find the
right elements and their content, the following shows how to do that
with VBScript in ASP using MSXML 4 and selectNodes, selectSingleNode. It
is also possible to solve that without XPath using getElementsByTagName
as needed.

Watch out for line breaks introduced by the post but not allowed in
VBScript:

<%@ Language="VBScript" %>
<html lang="en">
<head>
<title>Some feeds</title>
<script runat="server" language="VBScript">
Sub OutputFeed (feedURL)
Dim XmlDocument
Set XmlDocument = Server.CreateObject("Msxml2.DOMDocument.4.0")
XmlDocument.setProperty "ServerHTTPRequest", true
XmlDocument.async = False
Dim Loaded
Loaded = XmlDocument.load(feedURL)
If Loaded Then
Dim Items, Item, Title, Link, Description, TitleText,
DescriptionText, LinkURL
Set Items = XmlDocument.selectNodes("//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.selectSingleNode("title/text()")
If Title Is Nothing Then
TitleText = ""
Else
TitleText = Title.data
End If
Set Link = Item.selectSingleNode("link/text()")
If Link Is Nothing Then
LinkURL = ""
Else
LinkURL = Trim(Link.data)
End If
Set Description = Item.selectSingleNode("description/text()")
If Description Is Nothing Then
DescriptionText = ""
Else
DescriptionText = Description.data
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("http://rss.news.yahoo.com/rss/elections")
OutputFeed("http://www.cbsnews.com/feeds/rss/main.rss")
OutputFeed("http://rss.cnn.com/rss/si_topstories.rss")
%>

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top