可以使用jQuery的$(responseXML)语法可靠地解析XML吗? [英] Can XML be parsed reliably using jQuery's $(responseXML) syntax?

查看:65
本文介绍了可以使用jQuery的$(responseXML)语法可靠地解析XML吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在寻找一种使用JavaScript从服务器XML响应中提取信息的简便方法. jQuery似乎是一个不错的选择.

I'm currently looking for an easy way to extract information from server XML responses using JavaScript. jQuery seems like a good candidate for this.

在使用jQuery解析XML时,我不断遇到类似于以下代码段的代码示例:

When it comes to parsing XML with jQuery, I keep coming across code examples similar to the following snippet:

function parseXml(responseXml) {

    $(responseXml).find('someSelector')...

}

但是,jQuery Core文档(在下面的引用中)明确指出您不应该这样做:

Yet, the jQuery Core documentation (quotation below) clearly states that you aren't supposed to do that:

jQuery( html, [ ownerDocument ] )

html动态创建的HTML字符串.请注意,这会解析HTML,不是 XML.

html A string of HTML to create on the fly. Note that this parses HTML, not XML.

http://api.jquery.com/jQuery/#jQuery2

这使我想知道为什么仍然有如此多的在线资源建议通过$(responseXml)解析XML.尽管API文档说了什么,这通常可以正常工作吗?在什么情况下解析这样的XML实际上 不起作用?

This makes me wonder why so many online resources nevertheless suggest parsing XML via $(responseXml). Does this generally work without any problems, despite what the API documentation says? In what cases will parsing XML like this actually not work?

推荐答案

jQuery ajax文档添加了 http ://api.jquery.com/jQuery.ajax/:

the jQuery ajax documentation adds http://api.jquery.com/jQuery.ajax/:

数据类型

"xml":返回可以通过jQuery处理的XML文档.

"xml": Returns a XML document that can be processed via jQuery.

...如果服务器将返回数据报告为XML,则可以使用常规XML方法或jQuery的选择器遍历结果.

...If the server reports the return data as XML, the result can be traversed using normal XML methods or jQuery's selectors...

也在 http://api.jquery.com/jQuery/

当从Ajax调用返回XML数据时,我们可以使用$()函数将其包装在我们可以轻松使用的jQuery对象中.完成此操作后,我们可以使用.find()和其他DOM遍历方法来检索XML结构的各个元素.

When XML data is returned from an Ajax call, we can use the $() function to wrap it in a jQuery object that we can easily work with. Once this is done, we can retrieve individual elements of the XML structure using .find() and other DOM traversal methods.

这篇关于可以使用jQuery的$(responseXML)语法可靠地解析XML吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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