这是使用jQuery解析XML的错误吗? [英] Is this a bug in parsing XML, using jQuery?

查看:127
本文介绍了这是使用jQuery解析XML的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我会尝试尽可能简单地解释这个;

OK I'll try to explain this as simple as possible;

我正在尝试使用jQuery解析通过Web服务获得的XML。一切都在按照自己的意愿行事,直到我注意到为了上帝的爱,我无法解析一个名为image的节点。
长话短说(2小时后)我注意到,问题出在TAG NAME上。显然,图像是一个保留字或类似的东西???

I'm trying to parse XML that I get through web service, using jQuery. All is doing as it should, until I've noticed that for the love of God I can't parse a node that is named "image". long story short (and 2 hours later) I've noticed, that the problem is in the TAG NAME. Obviously, "image" is a reserved word or something like that???

这是一个演示;首先,我模拟了xml,它有图像节点,不会显示任何结果。其次,我模拟了SAME xml,SAME解析,我只是将image重命名为image1并且它工作。
请参阅此处的示例: http://jsbin.com/evinah/1/edit

Here's a demonstration; firstly, I simulated xml, that has "image" node and won't display any results. Secondly, I simulated the SAME xml, the SAME parsing, I just renamed "image" to "image1" and it WORKS. See the example here: http://jsbin.com/evinah/1/edit

所以,显然我无法更改网络服务,它不在我的域名中。如何使用jQuery获取图像值?

So, obviously I can't change the web service, it's not in my domain. How do I get the "image" value, using jQuery?

感谢您的回复。

推荐答案

您应该使用 $。parseXML 如果你正在处理一个字符串,这将返回一个可以与jQuery一起使用的有效的有效XML文档。

You should use $.parseXML if you are handling a string, this will return a valid valid XML document that can be used with jQuery.

$($.parseXML(xml)).find('item').each(function(i, e){
  alert($(e).children('image').text());
});

(在此处查看。)

如果您使用的是AJAX,则应设置 dataType xml 的选项。

If you are using AJAX then you should set the dataType option to xml.

<$的主要原因 $(xml)部分无需使用 $。parseXML ,但< image> ,这是因为< image> 元素在此期间被音译为< img> 元素DOM构建。最重要的是,< img> 是一个空元素,不会包含任何混乱XML结构的子文本元素。

The main reason why $(xml) partially works without using $.parseXML except for the <image>, it is because <image> elements are transliterated to <img> elements during the DOM construction. On top of it, <img> is an empty element and won't contain any children text elements which messes up the XML structure.

有关< img> < image> 历史用法的更多信息,请请参阅 < img> vs < image> HTML标记

For further information about the <img> and <image> historical usage please refer to <img> vs <image> tag in HTML.

这篇关于这是使用jQuery解析XML的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆