1

我已经搜索了几个小时的解决方案,但没有找到针对我的特定问题的解决方案。

我一直在试验这段代码:

$.ajax({
    url: 'https://gdata.youtube.com/feeds/api/users/profinsights/uploads',
    dataType: "xml",
    success: parseXml    
});

function parseXml(xml) {
    var entrynode = xml.getElementsByTagName("entry");
    $(entrynode).each(function() {
        //console.log($(this));
        console.log($('id',this)); //matches <id successfully
        console.log($('media:group',this)); // no match
    });
}

XML 片段:

...

<entry>
  <id>​http://gdata.youtube.com/feeds/api/videos/870MupRJhmU​&lt;/id>​
  <title type=​"text">​PIMS Humble Beginnings​&lt;/title>​
  <author>​…​&lt;/author>​
  <gd:comments>​…​&lt;/gd:comments>​
  <media:group>​
    <media:category label=​"Education" scheme=​"http:​/​/​gdata.youtube.com/​schemas/​2007/​categories.cat">​Education​&lt;/media:category>​
    <media:content url=​"https:​/​/​www.youtube.com/​v/​870MupRJhmU?version=3&f=user_uploads&app=youtube_gdata" type=​"application/​x-shockwave-flash" medium=​"video" isDefault=​"true" expression=​"full" duration=​"561" yt:format=​"5">​&lt;/media:content>​
    <media:content url=​"rtsp:​/​/​v3.cache3.c.youtube.com/​CigLENy73wIaHwllhkmUugy98xMYDSANFEgGUgx1c2VyX3VwbG9hZHMM/​0/​0/​0/​video.3gp" type=​"video/​3gpp" medium=​"video" expression=​"full" duration=​"561" yt:format=​"1">​&lt;/media:content>​
    <media:content url=​"rtsp:​/​/​v1.cache7.c.youtube.com/​CigLENy73wIaHwllhkmUugy98xMYESARFEgGUgx1c2VyX3VwbG9hZHMM/​0/​0/​0/​video.3gp" type=​"video/​3gpp" medium=​"video" expression=​"full" duration=​"561" yt:format=​"6">​&lt;/media:content>​
    <media:description type=​"plain">​…​&lt;/media:description>​
    <media:keywords>​…​&lt;/media:keywords>​
    <media:player url=​"https:​/​/​www.youtube.com/​watch?v=870MupRJhmU&feature=youtube_gdata_player">​&lt;/media:player>​
    <media:thumbnail url=​"http:​/​/​i.ytimg.com/​vi/​870MupRJhmU/​0.jpg" height=​"360" width=​"480" time=​"00:​04:​40.500">​&lt;/media:thumbnail>​
    <media:thumbnail url=​"http:​/​/​i.ytimg.com/​vi/​870MupRJhmU/​1.jpg" height=​"90" width=​"120" time=​"00:​02:​20.250">​&lt;/media:thumbnail>​
    <media:thumbnail url=​"http:​/​/​i.ytimg.com/​vi/​870MupRJhmU/​2.jpg" height=​"90" width=​"120" time=​"00:​04:​40.500">​&lt;/media:thumbnail>​
    <media:thumbnail url=​"http:​/​/​i.ytimg.com/​vi/​870MupRJhmU/​3.jpg" height=​"90" width=​"120" time=​"00:​07:​00.750">​&lt;/media:thumbnail>​
    <media:title type=​"plain">​PIMS Humble Beginnings​&lt;/media:title>​
    <yt:duration seconds=​"561">​&lt;/yt:duration>​
  </media:group>
​&lt;/entry>

...
  • 我的目标是从每个节点获取文本节点和属性,例如<media:foo>
  • 我知道我用于匹配节点 media:group的选择器是错误的,希望 xml 选择器方面的专家可以就如何处理这个问题给我一个建议。

先感谢您。

编辑:顺便说一句,这里是上述代码的小提琴:http: //jsfiddle.net/vYJwm/

更新

问题解决了。多谢你们。

4

1 回答 1

0

尝试这个:

http://jsfiddle.net/gRoberts/vYJwm/3/

jQuery 不会将它们解析为media:foo更多foo.

希望有帮助吗?

于 2012-07-19T13:03:13.160 回答