简体   繁体   English

在IE6中使用jQuery解析XML的问题

[英]Issues parsing XML with jQuery in IE6

I'm trying to extract values from xml using jQuery in a cross-browser compatible fashion. 我试图以跨浏览器兼容的方式使用jQuery从xml中提取值。 I'm not having any issues doing this in firefox, but unfortunately this also has to be IE compatible. 我在firefox中没有任何问题,但是不幸的是,这也必须与IE兼容。

My jQuery code looks like this: 我的jQuery代码如下所示:

$(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. 这将为我的xml文件中的<created>节点提取节点值。

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. 我在页面中引用了jQuery库,并且在Firefox中可以正确解析,因此我假设解析代码是适当的。 I get both alerts in Firefox, but only the first one in IE. 我在Firefox中收到两个警报,但在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? 有人可以在这里发现我在做什么错吗?

A few things: 一些东西:

  • Specify the response type as xml for your AJAX request 为您的AJAX请求指定响应类型为xml
  • Wrap the returning XML object in $(doc) and use find to query the XML 将返回的XML对象包装在$(doc)并使用find查询XML
  • I think you have a few typos in your first line: reader should be ready and you have an extra closing parentheses 我认为您在第一行中有一些错别字: reader应该已经ready并且您在括号中有一个额外的括号

This is working for me on IE6. 这在IE6上对我有用。 If this doesn't work for you, you may want to look into whether you are serving up your xml properly. 如果这对您不起作用,则可能要调查您是否正确提供了xml。

index.html : index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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"></script> 
  <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 : test.xml

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

try to wrap parseRefreshTime with $() 尝试用$()包装parseRefreshTime

    $("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')

    $(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 . 更新:另外,尝试将type指定为xml

$.get("file.xml", {}, <callback>, "xml")

确保将“ text / xml”用作xml文件的内容类型。

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. 基本上,尝试使用IE的Microsoft.XMLDOM方法。 can you provide sample xml? 您可以提供示例xml吗?

One of the biggest cave-eats with XML and IE6 is character encoding. XML和IE6的最大难题之一就是字符编码。 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. 与文档本身相比,您的网络服务器很可能为页面提供了不同的编码标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM