Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Accessing XML elements by name with ASP

Reply
Thread Tools

Accessing XML elements by name with ASP

 
 
gordon.lear@gmail.com
Guest
Posts: n/a
 
      03-18-2005
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!!

|
>>>-----O--------> E

|

-Gordon

glearATgmail.com

 
Reply With Quote
 
 
 
 
gmlear
Guest
Posts: n/a
 
      03-18-2005
My bad. Don't use glearATgmail.com it is old. Just reply to this
post. I have it starred. thx!!

 
Reply With Quote
 
 
 
 
William Park
Guest
Posts: n/a
 
      03-19-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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>

....
> 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>'

--
William Park <(E-Mail Removed)>, Toronto, Canada
Slackware Linux -- because it works.

 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      03-20-2005


(E-Mail Removed) 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/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>
--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
replacing xml elements with other elements using lxml Ultrus Python 4 08-30-2007 07:17 AM
Accessing elements of static array by name Paul Brettschneider C++ 8 08-27-2007 06:09 PM
XML elements to JavaScript Array elements Conversion P XML 1 07-07-2006 09:08 PM
XSLT: iterating all child elements and accessing homonymous childrenin sibling elements Gerald Aichholzer XML 2 06-27-2006 03:46 PM
Need Help accessing XML Elements with ASP/ADO gmlear XML 1 03-19-2005 04:28 AM



Advertisments