views:

344

answers:

5

I'm trying to extract values from xml using jQuery in a cross-browser compatible fashion. I'm not having any issues doing this in firefox, but unfortunately this also has to be IE compatible.

My jQuery code looks like this:

$(document).ready(function()) {
  $.get("file.xml", {}, function(parseRefreshTime){
    alert('This line is executed in IE.');  
    $("created", parseRefreshTime).each(function() {
      alert('This line is *not* executed in IE.');  
      refreshTime = $(this).text();
      //do stuff with refreshtime
    });
  });
});

This extracts the node value for a <created> node in my xml file.

I'm referencing the jQuery library in my page, and it's parsing properly in Firefox, so I'm assuming that that my parsing code is appropriate. I get both alerts in Firefox, but only the first one in IE.

I could swear I had very similar code working yesterday, but I must have tweaked something and somehow broken it. After fighting with it for almost an hour now, I'm looking for another set of eyes.

Can anyone spot what I'm doing wrong here?

+1  A: 

try to wrap parseRefreshTime with $()

    $("created", $(parseRefreshTime)).each(function() {
      alert('This line is *not* executed in IE.');  
      refreshTime = $(this).text();
      //do stuff with refreshtime
    });

or try to use $(parseRefreshTime).find('created')

    $(parseRefreshTime).find("created").each(function() {
      alert('This line is *not* executed in IE.');  
      refreshTime = $(this).text();
      //do stuff with refreshtime
    });

updated: also, try you specify the type to xml.

$.get("file.xml", {}, <callback>, "xml")
Anwar Chandra
No joy with either solution in IE6. :(
Peter Bernier
have you try to specify the type : "xml" ?
Anwar Chandra
Still no luck. Your suggestions work fine in FF, but I still can't get the value to parse successfully in IE.
Peter Bernier
+1  A: 

Make sure that 'text/xml' is used as the content-type for the xml file.

kgiannakakis
+3  A: 

A few things:

  • Specify the response type as xml for your AJAX request
  • Wrap the returning XML object in $(doc) and use find to query the XML
  • I think you have a few typos in your first line: reader should be ready and you have an extra closing parentheses

This is working for me on IE6. If this doesn't work for you, you may want to look into whether you are serving up your xml properly.

index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Test</title>

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt; 
  <script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
      $.get("test.xml", null, function(doc) {
        $(doc).find('created').each(function() {
          alert($(this).text());
        })
      }, 'xml');
    });
  </script>
</head>
<body>

</body>
</html>

test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<created>2010-01-07 00:00:00</created>
Lytol
A: 

I am using something like this:

    if ($.browser.msie){
        var tempXML = new ActiveXObject("Microsoft.XMLDOM");
        tempXML.async = false;
        tempXML.loadXML(data);
        xmlc = tempXML;
        items = $($(xmlc)[0]);
    } else if(window.DOMParser){
        items = $(new DOMParser().parseFromString(data, "text/xml").childNodes[0]);
    } else {
        xmlc = data;
        items = $($(xmlc)[1]);
    }

Basically, try the Microsoft.XMLDOM way for IE. can you provide sample xml?

Nakul
A: 

One of the biggest cave-eats with XML and IE6 is character encoding. Make sure your browser can interpret the file correctly. It could very well be your webserver is serving the page with a different encoding header in comparison to the document itself.

Bor