2009
04
May

XML Namespaces With jQuery (Solved)

If you have come to this post, you are in the same situation I was in. Many Javascript/AJAX framesworks like jQuery do not work with XML namespaces. I came upon an instance at work where I needed to display data from an RSS feed from Zoho Creator. Part of the problem was some of the nodes had default namespaces and others had custom namespaces. I needed to access the data in the custom namespaces and it just wouldn’t work in Firefox or IE. It worked just fine in Safari.

This is a sample output of the RSS feed from Zoho with the data replaced with dots:

<?xml version=”1.0″ encoding=”UTF-8″ ?>
<rss version=”2.0″ xmlns:zc=”http://creator.zoho.com/rss/”>
<channel>
<title>…</title>
<link>…</link>

<item>
<title>…</title>
<link>…link>
<description>…</description>
<guid isPermaLink=”true”>…</guid>
<zc:image>…</zc:image>
<zc:location>…</zc:location>
<zc:startdate>…</zc:startdate>
<zc:enddate>…</zc:enddate>
</item>

</channel>
</rss>

What I was doing with jQuery was looping over the “item” nodes using the jQuery each() method as such:

var items = $(data).find(“item”);
jQuery.each(items, function(i)
{
alert($(this).find(“location”).text())
});

The problem was that this only worked in Safari. In Firefox and IE nothing was returned. Firefox and IE don’t like the custom namespaces. The only way I have found to get this to work is to use this method:

var items = $(data).find(“item”);
jQuery.each(items, function(i)
{
alert($(this).find(“[@nodeName=zc:location]“).text())
});

6 Responses to XML Namespaces With jQuery (Solved)

  1. Liza says:

    Thanks, I needed this exact solution (in Safari, actually since I had multiple nodes with different namespaces and needed to select one and not the other).

  2. cravr says:

    This was a lifesaver! Thank you!

  3. vicente says:

    There’s nothing better than a good and succinct example. Congratulations!
    Thanks!

  4. pieczątka says:

    Good idea of article. I’m from Ireland I read many arts about it. I think idea of article is quite best and impresive to think about this article and future. Best regards from Denmark.

  5. ItsLeeOwen says:

    If you know the element type: $(‘myNamespace\\:div’).get()

  6. Everything typed made a ton of sense. But, what about this?
    what if you were to create a awesome headline?
    I ain’t suggesting your information isn’t good., however suppose you added
    something that makes people want more? I mean XML Namespaces With
    jQuery (Solved) | Witheringtree is a little boring.
    You should peek at Yahoo’s home page and see how they create news headlines to get viewers to open the links. You might try adding a video or a pic or two to grab readers excited about everything’ve got
    to say. Just my opinion, it might bring your posts a little livelier.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>