2012-07-19 66 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