Search code examples
jqueryxml-parsingjquery-xml

problems in xml parsing when the same element exists in different ns, using jquery


The xml contents are:

<?xml version="1.0"?>
<rss version="2.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007" xmlns:atom="http://www.w3.org/2005/Atom">
   <channel>
      <title>Some Title.</title>
      <description>Pipes Output</description>
      <link>http://pipes.yahoo.com/pipes/pipe.info</link>
      <atom:link rel="next" href="http://pipes.yahoo.com/pipes/pipe.run"/>
      <pubDate>Fri, 17 Aug 2012 07:25:28 +0000</pubDate>
      <generator>http://pipes.yahoo.com/pipes</generator>
      <item>
         <title>Title 1</title>
         <link>http://feedproxy.google.com</link>
         <description>there is no description for this</description>
         <guid isPermaLink="false">http://wordpress.com</guid>
         <pubDate>Thu, 16 Aug 2012 16:47:41 +0000</pubDate>
         <media:content medium="image" url="">
            <media:title type="html">some media</media:title>
         </media:content>
         <media:content medium="image" url="">
            <media:title type="html">Image</media:title>
         </media:content>
         <media:content medium="image" url="">
            <media:title type="html">Image</media:title>
         </media:content>
         <media:content medium="image" url="">
            <media:title type="html">Image</media:title>
         </media:content>
         <category>News</category>
      </item>        
      <item>
         <title>title 3</title>
         <link>http://google.com</link>
         <description>some description</description>
         <author>Self</author>
         <guid isPermaLink="false"></guid>
         <pubDate>Wed, 15 Aug 2012 17:43:32 +0000</pubDate>
         <enclosure type=""/>
      </item>

   </channel>
</rss>

As observed some items have <media:content> element and some doesnt have

Jquery to parse the xml is as follow:

function loadNews()
{
    var entries = [];
    var selectedEntry = "";
    $loadMore = $('#btn_loadmore');
    $loadMore.hide();
    var RSS = "news.xml";                       
//$.get(RSS, {}, function(res, code) {
    $.ajax(
    {
        type: "POST",
        url: "news.xml",                               
        dataType: "xml",
        async: false,
        success: function(res){
        var xml = $(res);
        var items = xml.find("item");
        $.each(items, function(i, v) {
        entry = {
           title:$(v).find("\\:title, title").text(),
           link:$(v).find("link").text().replace(/\/+$/, ""),
           description:$.trim($(v).find("description").text())
    };
     entries.push(entry);
    });

    var s = '';
    for(i=0;i<10;i++)
    {
        console.log(i + "--------------------------");
        if(i>=entries.length)
        break;
        alert(entries[i].title);
        console.log(entries[i].description);
        console.log(entries[i].link);
    }
    }
    });                                                
} 

The problem:

In firefox the above code works fine. However in safari the text inside media tags also gets appended in the title

output in firefox : Title 1

output in safari : Title 1some mediaImageImageImage

EDIT: how do i tell selector not to select <media:title> but only <title>

I did go through this post, but nothing helped.

What am i missing?

*I am using jquery 1.7.1, the problem is **same with jquery 1.6.****


Solution

  • Used find('[nodeName="title"]').text() instead of find("\\:title, title").text()

    But this works with jquery 1.6, i want to achieve the same with jquery 1.7.1