Accessing XML elements by name with ASP

Discussion in 'XML' started by gordon.lear@gmail.com, Mar 18, 2005.

  1. Guest

    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
    , Mar 18, 2005
    #1
    1. Advertising

  2. gmlear Guest

    My bad. Don't use glearATgmail.com it is old. Just reply to this
    post. I have it starred. thx!!
    gmlear, Mar 18, 2005
    #2
    1. Advertising

  3. William Park Guest

    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 <>, Toronto, Canada
    Slackware Linux -- because it works.
    William Park, Mar 19, 2005
    #3
  4. 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/
    Martin Honnen, Mar 20, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gmlear
    Replies:
    1
    Views:
    450
    earthling
    Mar 19, 2005
  2. Gerald Aichholzer
    Replies:
    2
    Views:
    2,492
    Gerald Aichholzer
    Jun 27, 2006
  3. P
    Replies:
    1
    Views:
    1,157
    Joe Kesselman
    Jul 7, 2006
  4. Paul Brettschneider

    Accessing elements of static array by name

    Paul Brettschneider, Aug 26, 2007, in forum: C++
    Replies:
    8
    Views:
    342
  5. Ultrus
    Replies:
    4
    Views:
    2,576
    Stefan Behnel
    Aug 30, 2007
Loading...

Share This Page