2012-07-19 56 views
1

我一直在Google上搜索几个小时的解决方案,但没有发现任何针对我的特定问题的方法。使用jquery选择<bar:foo>标记的XML标记

我一直在尝试使用此代码:

$.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​</id>​ 
    <title type=​"text">​PIMS Humble Beginnings​</title>​ 
    <author>​…​</author>​ 
    <gd:comments>​…​</gd:comments>​ 
    <media:group>​ 
    <media:category label=​"Education" scheme=​"http:​/​/​gdata.youtube.com/​schemas/​2007/​categories.cat">​Education​</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">​</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">​</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">​</media:content>​ 
    <media:description type=​"plain">​…​</media:description>​ 
    <media:keywords>​…​</media:keywords>​ 
    <media:player url=​"https:​/​/​www.youtube.com/​watch?v=870MupRJhmU&feature=youtube_gdata_player">​</media:player>​ 
    <media:thumbnail url=​"http:​/​/​i.ytimg.com/​vi/​870MupRJhmU/​0.jpg" height=​"360" width=​"480" time=​"00:​04:​40.500">​</media:thumbnail>​ 
    <media:thumbnail url=​"http:​/​/​i.ytimg.com/​vi/​870MupRJhmU/​1.jpg" height=​"90" width=​"120" time=​"00:​02:​20.250">​</media:thumbnail>​ 
    <media:thumbnail url=​"http:​/​/​i.ytimg.com/​vi/​870MupRJhmU/​2.jpg" height=​"90" width=​"120" time=​"00:​04:​40.500">​</media:thumbnail>​ 
    <media:thumbnail url=​"http:​/​/​i.ytimg.com/​vi/​870MupRJhmU/​3.jpg" height=​"90" width=​"120" time=​"00:​07:​00.750">​</media:thumbnail>​ 
    <media:title type=​"plain">​PIMS Humble Beginnings​</media:title>​ 
    <yt:duration seconds=​"561">​</yt:duration>​ 
    </media:group> 
​</entry> 

... 
  • 我的目标是让文本节点,并从名为 像<media:foo>每个节点属性
  • 我知道我的匹配节点的选择器 媒体:组是错误的,希望在XML选择器的专家可以给我一个关于如何处理这个问题的建议。

在此先感谢您。

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

UPDATE

问题解决了。多谢你们。

+0

[解析XML JQuery Ajax响应与命名空间](http://stackoverflow.com/questions/9625487/parsing-xml-jquery-ajax-response-with-namespace) - 适用于您的案例,使用' $( '媒体\\:组',这一点)'。 – 2012-07-19 12:31:53

+0

@RobW有问题,我根据您的建议更新了我的代码,但控制台日志输出为空。我不确定什么是错的。这里是更新的小提琴:http://jsfiddle.net/vYJwm/1/ – 2012-07-19 12:46:34

+0

@RobW我注意到,Google Chrome的控制台记录器没有给我预期的输出,所以我试着将结果重定向到文档,它给了我预期的输出在这里:http://jsfiddle.net/vYJwm/4/再次感谢ROB。 – 2012-07-19 13:11:51

回答

0

试试这个:

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

jQuery是不是把它们解析为media:foo,但更foo

希望有帮助吗?

+0

谢谢,但这不是我的目标。 Rob W已经给出了解决方案。再次感谢。 – 2012-07-19 13:12:35