开发者

ASP Xml Multiple Looping

开发者 https://www.devze.com 2023-03-30 07:24 出处:网络
How would I go about looping through XML in order to populate an image? I can loop through individual elements and it works, as shown below, but I want to access other elements from the same loop.

How would I go about looping through XML in order to populate an image?

I can loop through individual elements and it works, as shown below, but I want to access other elements from the same loop.

<%
    Dim xml, thumbnail, content, description, player, entry, title, list
    Set xml = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
    xml.async = False
    xml.SetProperty "ServerHTTPRequest", True
    xml.Load("http://gdata.youtube.com/feeds/api/users/Shuggy23/favorites?orderby=updated&max-results=15")

    Set entry = xml.getElementsByTagName("entry")
    Set thumbnail = xm开发者_StackOverflowl.getElementsByTagName("media:thumbnail")
    Set content = xml.getElementsByTagName("media:content")
    Set description = xml.getElementsByTagName("media:description")
    Set player = xml.getElementsByTagName("media:player")
    Set title = xml.getElementsByTagName("media:title")

    For Each xmlItem In thumbnail
        Response.Write xmlItem.getAttribute("url") & "<br />"
    Next

%>

I want to get values from other elements like title and player, but I have to use individual loops to get it to work. Is there any way to get them in the same loop or at the same time?

Thank you

Douglas


You can use XPATH inside Classic ASP

Dim xml, thumbnail, content, description, player, entry, title, list
Set xml = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xml.async = False
xml.SetProperty "ServerHTTPRequest", True
xml.Load("http://gdata.youtube.com/feeds/api/users/Shuggy23/favorites?orderby=updated&max-results=15")

set media_player = xml.selectNodes("feed[0]/entry[0]/media:group[0]/media:player[0]/@url")
if media_player.length > 0 then
    response.Write(media_player(0).Text)
else
    response.Write("not found")
end if

Here are a few examples that help with the basics of xpath: http://www.w3schools.com/xpath/xpath_examples.asp


Well you'll want to avoid using the getElementsByTagName method, there just no good way to access structured info using that method.

Sounds to like you first want to enumerate the entries in the feed:-

 For Each entry In xml.selectNodes("feed/entry")
   ''# Do stuff with an entry.
 Next

Now for each entry you want to get the title and the player url and I'll assume you actually only want one of the many possible thumbnail URLs.

 For Each entry In xml.selectNodes("feed/entry")

     Response.Write "Title: " & Server.HTMLEncode(entry.selectSingleNode("title").Text) & "<br />"
     Response.Write "Player URL: " & Server.HTMLEncode(entry.selectSingleNode("media:group/media:player/@url).Text) & "<br />"
     Response.Write "Thumbnail URL : " & Server.HTMLEncode(entity.selectSingleNode("media:group/media:thumbnail/@url").Text) & "<br />"

     Response.Write "<br />"
 Next
0

精彩评论

暂无评论...
验证码 换一张
取 消