使用 JQuery 使用 CDATA 解析 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/652159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing XML with CDATA with JQuery
提问by Rich Armstrong
Edit:I was missing two things here. The lack of "Content-Type:text/xml" in the header returned by the AJAX call was preventing JQuery from treating the returned data as a document. Once that was handled correctly, this code parsed correctly and output just the index and project name.
编辑:我在这里遗漏了两件事。AJAX 调用返回的标头中缺少“Content-Type:text/xml”阻止 JQuery 将返回的数据视为文档。一旦正确处理,此代码将正确解析并仅输出索引和项目名称。
$("a.getprojects").click(function(d){
d.preventDefault();
var api_token = $("#token").val();
var form_fbod = $("#fbod").val();
$.post("fbinfo.php", {fbod: form_fbod, token: api_token, cmd : 'listProjects', extra:''}, function(returned_xml) {
var output = '';
$(returned_xml).find("project").each(function(){
var project = $(this);
output += project.find("ixProject").text();
output += " ";
output += project.find("sProject").text();
output += "\n";
});
$("#output").val(output);
});
});
Original: I'm having fun using the FogBugz APIand JQuery to put together what I think will be a cool little tool, but I'm running into a JQuery limitation. CDATA tags seem to confuse it.
原文:我很高兴使用FogBugz API和 JQuery 将我认为将是一个很酷的小工具放在一起,但我遇到了 JQuery 限制。CDATA 标签似乎混淆了它。
Here's the code I'm using:
这是我正在使用的代码:
$("a.getprojects").click(function(d){
d.preventDefault();
var api_token = $("#token").val();
var form_fbod = $("#fbod").val();
$.post("fbinfo.php", {fbod: form_fbod, token: api_token, cmd : 'listProjects', extra:''}, function(xml) {
var output = xml;
$(xml).find("project").each(function(){
var project = $(this);
output += "\n\n";
output += project.html();
});
$("#output").val(output);
});
});
And here 's the output I get:
这是我得到的输出:
<?xml version="1.0" encoding="UTF-8"?><response>
<projects>
<project>
<ixProject>2</ixProject>
<sProject><![CDATA[Inbox]]></sProject>
<ixPersonOwner>2</ixPersonOwner>
<sPersonOwner><![CDATA[Rich]]></sPersonOwner>
<sEmail><![CDATA[[email protected]]]></sEmail>
<sPhone></sPhone>
<fInbox>true</fInbox>
<ixGroup>1</ixGroup>
<iType>1</iType>
<sGroup><![CDATA[Internal]]></sGroup>
</project>
<project>
<ixProject>1</ixProject>
<sProject><![CDATA[Sample Project]]></sProject>
<ixPersonOwner>2</ixPersonOwner>
<sPersonOwner><![CDATA[Rich]]></sPersonOwner>
<sEmail><![CDATA[[email protected]]]></sEmail>
<sPhone></sPhone>
<fInbox>false</fInbox>
<ixGroup>1</ixGroup>
<iType>1</iType>
<sGroup><![CDATA[Internal]]></sGroup>
</project>
</projects>
</response>
<ixproject>2</ixproject>
<sproject></sproject>
<ixpersonowner>2</ixpersonowner>
<spersonowner></spersonowner>
<semail></semail>
<sphone></sphone>
<finbox>true</finbox>
<ixgroup>1</ixgroup>
<itype>1</itype>
<sgroup></sgroup>
<ixproject>1</ixproject>
<sproject></sproject>
<ixpersonowner>2</ixpersonowner>
<spersonowner></spersonowner>
<semail></semail>
<sphone></sphone>
<finbox>false</finbox>
<ixgroup>1</ixgroup>
<itype>1</itype>
<sgroup></sgroup>
It would seem that the XML parsing that's native to JQuery discards the contents of CDATA elements. FogBugz puts most of our string data in CDATA tags because we allow special characters and punctuation in most places. Enclosing the output in CDATA tags allows us to rest relatively assured that we're sending back valid data via our API. PHP parsing of the XML works just fine. My research online yields a few people complaining about this, but not much work getting done. With JQuery's extensibility, I would think that there's something out there. Has anyone else accomplished this?
JQuery 原生的 XML 解析似乎丢弃了 CDATA 元素的内容。FogBugz 将我们的大部分字符串数据放在 CDATA 标签中,因为我们允许在大多数地方使用特殊字符和标点符号。将输出包含在 CDATA 标签中让我们可以相对放心,我们正在通过我们的 API 发回有效数据。XML 的 PHP 解析工作正常。我在网上的研究让一些人抱怨这个,但没有多少工作完成。有了 JQuery 的可扩展性,我会认为那里有一些东西。有没有其他人完成过这个?
回答by bobince
It would seem that the XML parsing that's native to JQuery
似乎 JQuery 原生的 XML 解析
There is no XML parsing native to jQuery. It just uses the standard XMLHttpRequest.responseXML property to get an XML DOM for the response.
jQuery 没有原生的 XML 解析。它只是使用标准的 XMLHttpRequest.responseXML 属性来获取响应的 XML DOM。
discards the contents of CDATA elements
丢弃 CDATA 元素的内容
What Content-Type are you sending the response with? Because I suspect it's not being parsed as XML at all. In this case jQuery will be passing you back a string of the document, instead of an XML DOM.
你用什么 Content-Type 发送响应?因为我怀疑它根本没有被解析为 XML。在这种情况下,jQuery 将向您传回文档字符串,而不是 XML DOM。
Then when you call “$(xml)”, it will be creating document content from that string(*)?—?parsed as HTML, notXML. In HTML there is no such thing as a CDATA section, so browsers might discard them, or treat them as comments.
然后当你调用“$(xml)”时,它会从那个字符串(*)?—?解析为HTML而不是XML来创建文档内容。在 HTML 中没有 CDATA 部分这样的东西,因此浏览器可能会丢弃它们,或者将它们视为注释。
I suspect this because “project.html()” shouldn't actually work when the document is XML. ‘html()' just returns the same as the standard ‘innerHTML' property(**), which only works for HTML documents; it is undefined on XML elements.
我怀疑这是因为当文档是 XML 时,“project.html()”实际上不应该工作。'html()' 只返回与标准的 'innerHTML' 属性(**) 相同的值,它只适用于 HTML 文档;它在 XML 元素上未定义。
Enclosing the output in CDATA tags allows us to rest relatively assured that we're sending back valid data via our API.
将输出包含在 CDATA 标签中让我们可以相对放心,我们正在通过我们的 API 发回有效数据。
Well, ‘relatively': if your data happens to contain “]]>” you still lose. <![CDATA[ sections are intended as a crutch to improve writability for hand authoring; machine-generated XML should really just use entity-encoding in the normal fashion. Usually the server app should be using proper XML tools to generate the response in which case this will be done automatically.
好吧,“相对”:如果您的数据碰巧包含“]]>”,您仍然会失败。<![CDATA[ 部分旨在作为提高手工创作的可写性的拐杖;机器生成的 XML 应该真正以正常方式使用实体编码。通常服务器应用程序应该使用适当的 XML 工具来生成响应,在这种情况下,这将自动完成。
(*: I have never understood when jQuery feels the need to squish document fragment creation and CSS selection into the same function. They're completely different operations which you don't want to get confused, as may have happened here.)
(*:我从来不明白 jQuery 什么时候觉得需要将文档片段创建和 CSS 选择压缩到同一个函数中。它们是完全不同的操作,您不想混淆,就像这里发生的那样。)
(**: Actually, it tries to filter out jQuery custom attributes first, using a regex. Unfortunately since regex cannot parse HTML, it will happily filter out valid parts of your text that happen to look like HTML attributes. Whoops. Not one of jQuery's prettier parts.)
(**: 实际上,它首先尝试使用正则表达式过滤掉 jQuery 自定义属性。不幸的是,由于正则表达式无法解析 HTML,它会很高兴地过滤掉您的文本中碰巧看起来像 HTML 属性的有效部分。哎呀。不是其中之一jQuery 更漂亮的部分。)
回答by CharlesTWall3
jquery does in fact have an xml parser now that should solve your problem. $.parseXML(xml) http://api.jquery.com/jQuery.parseXML/
jquery 实际上现在有一个 xml 解析器可以解决您的问题。$.parseXML(xml) http://api.jquery.com/jQuery.parseXML/