5

XML:

<item>
     <title>Some title</title>

     <description>
     <![CDATA[
      Some description text Some description text Some description text Some description text Some description text Some description text 
      Some description text Some description text Some description text Some description text Some description text Some description text 
      Some description text Some description text Some description text Some description text Some description text Some description text 
      Some description text Some description text Some description text Some description text Some description text Some description text 
     ]]>
     </description>

     <media:content isDefault="true" medium="image" url="http://sampledomain.com/image.jpg">
      <media:title>Image title</media:title>
      <media:description>Image description</media:description>
      <media:thumbnail url="http://sampledomain.com/image.jpg" width="160" height="120" />
     </media:content>

     <wfw:commentRss>http://sampledomain.com/comment/feed/</wfw:commentRss>
     <slash:comments>0</slash:comments>
    </item>

I am fetching css from ajax and in onsuccess function my code is as follow:

$(xml).find('item').each(function() {
 var title = $(this).find('title').eq(0).text();
 var url   = $(this).find('content').attr('url');

 // Printing url as undefined
 alert(url);
 console.log($(this).find('content');

});

What I want to get content url attribute I am getting item children (title, description, content, commentRss and comments) but when I try to get $(this).find('content') It is not giving me anything Can anyone figure it out what am I doing wrong? Thanks

shankhan
  • 6,343
  • 2
  • 19
  • 22
  • 1
    var url=$(this).find('content').attr('url').val(); or try using .text() hope it helps – Rafay Jan 28 '11 at 07:49

2 Answers2

4

Your XML uses a namespace.

See this similar Stack Overflow question regarding namespaces. You have to escape the colon:

jQuery XML parsing with namespaces

Community
  • 1
  • 1
jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
  • thanks for the link this works for me $(this).find('media\\:content').attr('url'); – shankhan Jan 28 '11 at 07:53
  • @shankhan - You're welcome. This is definitely good to know. I don't deal with namespaces that often, but am planning a project myself that will involve them. – jamesmortensen Jan 29 '11 at 17:56
1

Got a correction for you.. and your Namespace SOLUTION! You need to escape the namespace ":" properly with \. I have show the example below which works with the Itunes podcast XML feeds.

 item.description = jQuery(this).find("[nodeName=itunes\\:summary]").eq(0).text();
John Drefahl
  • 556
  • 3
  • 17