使用jquery </bar:foo>选择带有<bar:foo>标记的XML标记

时间:2012-07-19 12:28:09

标签: jquery dom xml-parsing

我一直在谷歌搜索解决方案几个小时,但没有发现任何针对我的具体问题。

我一直在试验这段代码:

$.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/

更新

问题已解决。谢谢你们。

1 个答案:

答案 0 :(得分:0)

试试这个:

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

jQuery没有将它们解析为media:foo,而是更多foo

希望有帮助吗?