www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] Ajax - XML "link" tag not returning anything from jQuery.find()

Hybrid View

  1. #1
    Join Date
    Dec 2008
    Posts
    488

    resolved [RESOLVED] Ajax - XML "link" tag not returning anything from jQuery.find()

    I'm trying to read and XML file, and one of my tags is <link></link>. When I use jQuery.find('link').text(), I get an empty string. All the other tags are returning correct text. Also, when I simply alert(inputXML), everything is there and correct:

    HTML Code:
    <?xml version="1.0"?>
    <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
        <channel>
            <title>New Indie Bands</title>
            <link>http://www.newindiebands.com/indie-bands/</link>
            <description>New Indie Bands</description>
            <atom:link href="http://www.newindiebands.com/backlinks.xml" rel="self" type="application/rss+xml" />
            <item>
                <title>Indie Bands</title>
                <description></description>
                <link>http://amiestreet.com/user/indiebands/</link>
                <guid>http://amiestreet.com/user/indiebands/</guid>
            </item>
            <item>
                <title>Indie Bands</title>
                <description></description>
                <link>http://www.artslant.com/ny/artists/show/127323-indie-bands</link>
                <guid>http://www.artslant.com/ny/artists/show/127323-indie-bands</guid>
            </item>
        </channel>
    </rss>
    Code:
    $(inputXML).find('link').each(function() {
        alert($(this).text());
    });
    Any reason why jQuery wouldn't return anything for a link tag in XML? Any way to fix it without changing the XML?

  2. #2
    Join Date
    Dec 2008
    Posts
    488
    Also, I felt it work mentioning that when I had my XML in a string and converted it to XML using DOMparser.parseFromString(xmlDoc, "text/xml"), everything was working perfectly. The moment I started using the ajax to get an actual XML file, it broke. So I wonder if it may have something to do with the version of XML returned?

  3. #3
    Join Date
    Jul 2005
    Location
    USA
    Posts
    912
    $(inputXML).find('atom\\:link').each(function() {
    alert($(this).text());
    });

  4. #4
    Join Date
    Dec 2008
    Posts
    488
    I'm not working with that one, only with the actual <link> tags inside the <item> nodes.

  5. #5
    Join Date
    Dec 2008
    Posts
    488
    Hey, I figured it out. I was setting my jQuery.ajax() option "dataType" to "text/xml", when it should have been "xml" alone. I'm not sure why this affected it, but it did. If anyone's got any insight, I'd love to hear it. I suspect it might have been because jQuery was reading it as an HTML link tag, which is supposed to have no inner content. Calling this one resoved. Thanks.

  6. #6
    Join Date
    Jul 2005
    Location
    USA
    Posts
    912
    It could be a browser issue. I found this post:

    Code:
    jQuery.fromXMLString = function(strXML){
        if (window.DOMParser) {
            return jQuery(new DOMParser().parseFromString(strXML, "text/xml"));
        } else if (window.ActiveXObject) {
            var doc = new ActiveXObject("Microsoft.XMLDOM");
            doc.async = "false";
            doc.loadXML(strXML);
            return jQuery(doc);
        } else {
            return jQuery(strXML);
        }
    };
    Using that method call and it appears IE doesn't support it (unless it has been added recently). Are you using IE? Is your Ajax instance returning XML or Text? That could also be a reason. Are you passing {dataType:'text'}?
    Last edited by Webnerd; 01-06-2010 at 07:06 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles