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.
我目前正在寻找一种使用JavaScript从服务器XML响应中提取信息的简单方法。 jQuery似乎是一个很好的候选人。
When it comes to parsing XML with jQuery, I keep coming across code examples similar to the following snippet:
在使用jQuery解析XML时,我不断遇到类似于以下代码段的代码示例:
function parseXml(responseXml) {
$(responseXml).find('someSelector')...
}
Yet, the jQuery Core documentation (quotation below) clearly states that you aren't supposed to do that:
然而,jQuery Core文档(下面引用)明确指出你不应该这样做:
jQuery( html, [ ownerDocument ] )
jQuery(html,[ownerDocument])
html
A string of HTML to create on the fly. Note that this parses HTML, not XML.html即时创建的HTML字符串。请注意,这解析HTML而不是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?
这让我想知道为什么这么多的在线资源建议通过$(responseXml)解析XML。尽管API文档说的是什么,这通常没有任何问题吗?在什么情况下解析这样的XML实际上不起作用?
3 个解决方案
#1
3
the jQuery ajax documentation adds http://api.jquery.com/jQuery.ajax/:
jQuery ajax文档添加了http://api.jquery.com/jQuery.ajax/:
dataType
"xml": Returns a XML document that can be processed via jQuery.
“xml”:返回可以通过jQuery处理的XML文档。
...If the server reports the return data as XML, the result can be traversed using normal XML methods or jQuery's selectors...
...如果服务器将返回数据报告为XML,则可以使用普通XML方法或jQuery选择器遍历结果...
Also at http://api.jquery.com/jQuery/
也在http://api.jquery.com/jQuery/
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.
当从Ajax调用返回XML数据时,我们可以使用$()函数将其包装在我们可以轻松使用的jQuery对象中。完成此操作后,我们可以使用.find()和其他DOM遍历方法检索XML结构的各个元素。
#2
4
I do not know if my experience can be generalized, but I had my share of problems parsing SOAP messages with jQuery. This is probably not down to jQuery (as you point out the documentation disrecommends it).
我不知道我的经验是否可以概括,但我有一些问题,用jQuery解析SOAP消息。这可能不是jQuery(正如你指出文档反对它)。
Anyway, you asked for specifics: I found everything with namespaces to be problematic. Of course, for genuine namespace awareness, you'd need something that can resolve namespace prefixes to a namespace URI. I never expected jQuery to be able to do that, but even matching only prefixes didn't work out for me. This is especially problematic for me because the SOAP messages I am dealing with easily mix 4 or 5 namespaces. So I went back to doing DOM traversal myself to tackle this problem (which has its own set of problems)
无论如何,你要求具体细节:我发现名称空间的一切都有问题。当然,对于真正的命名空间感知,您需要能够将命名空间前缀解析为命名空间URI的东西。我从没想过jQuery能够做到这一点,但即使只匹配前缀也不适合我。这对我来说尤其成问题,因为我正在处理的SOAP消息很容易混合4或5个命名空间。所以我回到自己做DOM遍历来解决这个问题(它有一系列问题)
That said, I do expect jQuery to be able to handle xhtml documents (as long as the tag names aren't prefixed), and I would expect it to work too for other xml documents that do not use namespace prefixes.
也就是说,我确实希望jQuery能够处理xhtml文档(只要标签名称没有前缀),我希望它也能用于其他不使用名称空间前缀的xml文档。
#3
3
Your link actually points to the usage of jQuery( html )
that deals with creation of elements from strings, i.e.
您的链接实际上指向jQuery(html)的使用,该jQuery处理从字符串创建元素,即
$('<a href="..."></a>')
The one that your code uses is jQuery( element )
which is fine for XML.
您的代码使用的是jQuery(element),它适用于XML。
since responseXML is XML, not a string, you're free to use jQuery()
on it.
因为responseXML是XML而不是字符串,所以你可以*地使用jQuery()。
#1
3
the jQuery ajax documentation adds http://api.jquery.com/jQuery.ajax/:
jQuery ajax文档添加了http://api.jquery.com/jQuery.ajax/:
dataType
"xml": Returns a XML document that can be processed via jQuery.
“xml”:返回可以通过jQuery处理的XML文档。
...If the server reports the return data as XML, the result can be traversed using normal XML methods or jQuery's selectors...
...如果服务器将返回数据报告为XML,则可以使用普通XML方法或jQuery选择器遍历结果...
Also at http://api.jquery.com/jQuery/
也在http://api.jquery.com/jQuery/
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.
当从Ajax调用返回XML数据时,我们可以使用$()函数将其包装在我们可以轻松使用的jQuery对象中。完成此操作后,我们可以使用.find()和其他DOM遍历方法检索XML结构的各个元素。
#2
4
I do not know if my experience can be generalized, but I had my share of problems parsing SOAP messages with jQuery. This is probably not down to jQuery (as you point out the documentation disrecommends it).
我不知道我的经验是否可以概括,但我有一些问题,用jQuery解析SOAP消息。这可能不是jQuery(正如你指出文档反对它)。
Anyway, you asked for specifics: I found everything with namespaces to be problematic. Of course, for genuine namespace awareness, you'd need something that can resolve namespace prefixes to a namespace URI. I never expected jQuery to be able to do that, but even matching only prefixes didn't work out for me. This is especially problematic for me because the SOAP messages I am dealing with easily mix 4 or 5 namespaces. So I went back to doing DOM traversal myself to tackle this problem (which has its own set of problems)
无论如何,你要求具体细节:我发现名称空间的一切都有问题。当然,对于真正的命名空间感知,您需要能够将命名空间前缀解析为命名空间URI的东西。我从没想过jQuery能够做到这一点,但即使只匹配前缀也不适合我。这对我来说尤其成问题,因为我正在处理的SOAP消息很容易混合4或5个命名空间。所以我回到自己做DOM遍历来解决这个问题(它有一系列问题)
That said, I do expect jQuery to be able to handle xhtml documents (as long as the tag names aren't prefixed), and I would expect it to work too for other xml documents that do not use namespace prefixes.
也就是说,我确实希望jQuery能够处理xhtml文档(只要标签名称没有前缀),我希望它也能用于其他不使用名称空间前缀的xml文档。
#3
3
Your link actually points to the usage of jQuery( html )
that deals with creation of elements from strings, i.e.
您的链接实际上指向jQuery(html)的使用,该jQuery处理从字符串创建元素,即
$('<a href="..."></a>')
The one that your code uses is jQuery( element )
which is fine for XML.
您的代码使用的是jQuery(element),它适用于XML。
since responseXML is XML, not a string, you're free to use jQuery()
on it.
因为responseXML是XML而不是字符串,所以你可以*地使用jQuery()。