【问题标题】:jQuery parse xml not returning data in iejQuery 解析 xml 在 ie 中不返回数据
【发布时间】:2010-02-16 11:45:41
【问题描述】:

我在 ajax 调用后解析 xml 文件以“获取”xml 文件。我正在尝试返回其中一个节点(使用 find 方法)。但是它不会返回 ie 中的值 - 在 Firefox 中很好。我正在发出警报以测试该值,但它只是返回空白。下面是代码:

function autoFeedGetter () {
 $('#notifyBox').empty();
  $.ajax ({
  type: "GET",
  url: "actual url removed",    
  datatype: "xml",
  success: checkNoteDate 
  }); 
  };
 // get time date saved to fullDateTime Variable 

 function checkNoteDate (xml) {
  var noteDate = 'Not';
  var lastSplit = 'Not'; 
  // get published date from feed
  noteDate = $(xml).find('entry published:eq(0)').text();    
  alert(noteDate);
}

问题似乎出在这个方法上——noteDate = $(xml).find('entry published:eq(0)').text();

但是我已经尝试了其他各种版本,包括: noteDate = $(xml).find('entry:first published').text();

如果有人有任何信息将不胜感激。

示例 xml

    <?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:ibmfs="http://purl.org/net/ibmfeedsvc/feedsvc/1.0" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <title>Notifications</title>
  <link rel="alternate" href="http://www.#.com/notifyme" />
  <subtitle>This feed has been created using ROME (Java syndication utilities)</subtitle>
  <entry>
    <title>This is another test message !</title>

    <link rel="alternate" href="http://#/cwweb/" />
    <author>
      <name>ContentWatch</name>
    </author>
    <updated>2010-02-12T14:22:19Z</updated>
    <published>2010-02-12T14:22:19Z</published>
    <dc:creator>ContentWatch</dc:creator>

    <dc:date>2010-02-12T14:22:19Z</dc:date>
  </entry>
  <entry>
    <title>You got mail!</title>
    <link rel="alternate" href="http://#/cwweb/" />
    <author>
      <name>ContentWatch</name>

    </author>
    <updated>2010-02-10T12:11:49Z</updated>
    <published>2010-02-10T12:11:49Z</published>
    <dc:creator>ContentWatch</dc:creator>
    <dc:date>2010-02-10T12:11:49Z</dc:date>
  </entry>
</feed>

已删除敏感数据 - 我能够成功解析完整数据但无法检索一个元素 - 例如我正在寻找第一个发布日期

【问题讨论】:

  • 你能举一个你正在下载的xml的例子吗?
  • 可能只是一个错字,但.ajax()datatype 选项应该是dataType,而不是datatype
  • Cheers Crescent Fresh 确实是一个很好发现的错字:-)
  • @jonnyhitek 你找到这个问题的答案了吗,因为我也有同样的问题

标签: javascript jquery xml


【解决方案1】:

查看 http://groups.google.com/group/jquery-en/browse_thread/thread/adb2b047f3761179?pli=1 底部的 andrea varnier 的回复,IE 似乎无法处理从本地计算机加载的 xml 文件。如果您不想在部署页面时依赖它,这里还有一个解决方案邮资:

success: function(xmlData){ 
var data; 
if ( typeof xmlData == 'string') { 
    data = new ActiveXObject( 'Microsoft.XMLDOM'); 
    data.async = false; 
    data.loadXML( xmlData); 
} else { 
    data = xmlData; 
}

所以看起来 IE 将 XML 保留为单个字符串,因此该方法将其加载到 xml 文档对象中。然后,您可以在该对象上使用 jQuery。

【讨论】:

  • 干杯保罗我已经尝试过 noteDate = $(xml).find('entry published:first').text() 这在 Firefox 中很好,但在 ie 中返回空白将尝试你的第二个建议和让你知道我是怎么相处的。欢呼
  • 我已经用我在 Google 上找到的潜在解决方案编辑了答案。
  • 很酷,谢谢你的额外努力,保罗我会试一试,让你知道结果:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多