views:

1107

answers:

2

Hi. Thanks for reading.

I'm trying to parse an xml response from YouTube but I'm completely blocked, hehe.

Well, What I've got until now is this:

<%

    Option Explicit
    Response.Buffer = True

    Dim videoVimeo, videoYoutube
     videoVimeo = "http://vimeo.com/5866977"
     videoYoutube = "http://www.youtube.com/watch?v=d8nxjUlbKJA"

'------------------------------------------------------------------
'----------------------- YouTube request --------------------------
'------------------------------------------------------------------
     ' replacing the url to get the ID from the video
     videoYoutube = Replace(videoYoutube,"http://www.youtube.com/watch?v=","")

     ' pasting the ID to the api URL provided from YouTube
     videoYoutube = "http://gdata.youtube.com/feeds/api/videos/"&amp;videoYoutube

    Dim xml
     set xml = Server.CreateObject("Microsoft.XMLHTTP")
     set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")

     xml.Open "GET", videoYoutube, False

     On Error Resume Next

     xml.Send

    Dim docXml
     Set docXml = Server.CreateObject("msxml2.DOMDocument")
      docXml.loadxml(xml.ResponseText)

    Set xml = nothing

%>

Ok, from here I don't know how to parse the response.

What I want to do is to save the nodes into variables such as title, date uploaded, rating, etc.

I tried this one http://www.aspmessageboard.com/showthread.php?t=230539 to solve the problem, but I was not able to get every node into variables.

Thanks for your help!

A: 

First you need to setup the namespace as you will not be able to Xpath queries without it. This may work depending on the version of MSXML that is installed on your server.

oXslt.setProperty "SelectionNamespaces", "xmlns:atom='http://www.w3.org/2005/Atom'"

If this does not work you probably need to create a MXNamespaceManager

Set oNSMgr = Server.CreateObject("MSXML2.MXNamespacemanager")
oNSMgr.declarePrefix "atom", "http://www.w3.org/2005/Atom"

I'm not sure how you associate the Namespace Manager with the DOMDocument. Maybe you don't have to!

To get access to your data, you can now do Xpath queries using your new prefix.

set titlenode = docXml.SelectSingleNode("/atom:entry/atom:title")
title = titlenode.Text

set publishednode = docXml.SelectSingleNode("/atom:entry/atom:published")
publishednode  = publishednode .Text

To get at the rating you will have to add a new namespace,

xmlns:gd='http://schemas.google.com/g/2005'

and get at it like this

set ratingnode = docXml.SelectSingleNode("/atom:entry/gd:rating")
ratingnode  = ratingnode.Text

Hope that helps, and do us all a favour and stop using classic ASP!

Darrel Miller
"do us all a favour and stop using classic ASP", not helpful. It only serves to make the OP and others of us who still use it for various good commercial reasons to feel bad. Clearly the superior amoungst us would stop using ASP. By way of a challenge re-write your code above in .NET and demonstrate how it would be better?
AnthonyWJones
I still support 500,000 lines of VB6 code. I was not attempting to slight the person, just give them a gentle nudge in case their pet project was using language that they happen to already know!I've done enough work with XML in both VB6 and .Net to know the .Net libraries are significantly better.
Darrel Miller
@Darrel: Fair enough. If you are refering to the new Linq-To-XML stuff then I would agree with you. However the standard DOM implementation in .NET would result in very similar code that already has been posted, in fact it would be clunkier than this existing MSXML based solution.
AnthonyWJones
@AnthonyWJones You are right in that the interface for XmlDocument is not much better than DOMDocument, however, the catastrophe that is MSXML2,3,4 (sp1,2,3,4), 5, 6, etc makes using the COM based XML parser a much less pleasant experience.
Darrel Miller
A: 

This code assumes that XmlHttp recognizes application/atom+xml to be an XML mime type and hence ResponseXML will have a DOM loaded.

<%

    Option Explicit
    Response.Buffer = True

    Dim videoVimeo : videoVimeo = "http://vimeo.com/5866977"

    Dim videoYoutube : videoYoutube = "http://www.youtube.com/watch?v=d8nxjUlbKJA"

    videoYoutube = Replace(videoYoutube,"http://www.youtube.com/watch?v=","")

    videoYoutube = "http://gdata.youtube.com/feeds/api/videos/" & videoYoutube


    Dim xhr: Set xhr= Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")

    xhr.Open "GET", videoYoutube, False

    xhr.Send

    If xhr.Status = 200 Then
        Dim xml : Set xml = xhr.ResponseXML
        xml.SetProperty "SelectionLanguage", "XPath"
        Dim ns : ns = "xmlns:a='http://www.w3.org/2005/Atom' "
        ns = ns & "xmlns:gd='http://schemas.google.com/g/2005' "
        xml.SetProperty "SelectionNamespaces", ns

        Dim entry : Set entry = xml.DocumentElement

        Dim title : title = entry.SelectSingleNode("a:title").Text
        Dim published : published = entry.SelectSingleNode("a:published")
        Dim rating : rating = entry.SelectSingleNode("gd:rating").GetAttribute("average")


    End If
%>

If the mime type is not recognized as xml the ReponseXML property will be Nothing. In this case the ResponseStream property can be used to load a DOM:-

Dim xml : Set xml = Server.CreateObject("MSXML2.DOMDocument.3.0")
xml.load xhr.ResponseStream
AnthonyWJones
Hi. I'm getting an error:msxml3.dll error '80004005' Required whitespace is missing.What could it be?Thank you very much!
Needed to end the ns variables with an extra space, I've tweaked the answer.
AnthonyWJones