代码之家  ›  专栏  ›  技术社区  ›  jos

使用ASP Classic解析Server.XMLHTTP YouTube响应

  •  1
  • jos  · 技术社区  · 16 年前

    好吧,到现在为止我得到的是:

    <%
    
        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/"&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
    
    %>
    

    好吧,从这里我不知道如何解析响应。

    我要做的是将节点保存为变量,如标题、上传日期、评分等。

    我试过这个 http://www.aspmessageboard.com/showthread.php?t=230539 为了解决这个问题,但我无法将每个节点都转换成变量。

    2 回复  |  直到 16 年前
        1
  •  0
  •   Darrel Miller    16 年前

    首先,您需要设置名称空间,因为没有名称空间,您将无法执行Xpath查询。

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

    如果这不起作用,您可能需要创建一个MXNamespaceManager

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

    我不确定如何将命名空间管理器与DOMDocument相关联。也许你不必!

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

    要达到这个级别,您必须添加一个新的名称空间,

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

    像这样做

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

        2
  •  0
  •   AnthonyWJones    16 年前

    这段代码假设XmlHttp将application/atom+xml识别为xml mime类型,因此 ResponseXML

    <%
    
        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
    %>
    

    如果无法将mime类型识别为xml,ReponseXML属性将为空。在这种情况下,ResponseStream属性可用于加载DOM:-

    Dim xml : Set xml = Server.CreateObject("MSXML2.DOMDocument.3.0")
    xml.load xhr.ResponseStream