使用JQuery使用CDATA解析XML [英] Parsing XML with CDATA with JQuery

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

问题描述

编辑:我在这里错过了两件事. AJAX调用返回的标头中缺少"Content-Type:text/xml",这阻止了JQuery将返回的数据视为文档.处理正确后,此代码将正确解析,仅输出索引和项目名称.

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.

$("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);
  });
});

原始:我正在使用 FogBugz API 和JQuery组合在一起,我认为这将是一个很棒的小工具,但我遇到了JQuery的局限性. CDATA标记似乎使它感到困惑.

Original: I'm having fun using the FogBugz API and 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.

这是我正在使用的代码:

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);

    });
  });

这是我得到的输出:

<?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[rich@example.com]]></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[rich@example.com]]></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>

似乎JQuery固有的XML解析会丢弃CDATA元素的内容. FogBugz将大多数字符串数据放入CDATA标记中,因为我们在大多数地方都允许特殊字符和标点符号.将输出包含在CDATA标记中,使我们可以相对放心地确保我们通过API发送回有效数据. XML的PHP​​解析工作正常.我在网上进行的研究使一些人对此表示抱怨,但没有完成很多工作.有了JQuery的可扩展性,我会认为那里有些东西.还有其他人做到过吗?

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解析

It would seem that the XML parsing that's native to JQuery

没有jQuery本身的XML解析.它只是使用标准的XMLHttpRequest.responseXML属性来获取响应的XML DOM.

There is no XML parsing native to jQuery. It just uses the standard XMLHttpRequest.responseXML property to get an XML DOM for the response.

丢弃CDATA元素的内容

discards the contents of CDATA elements

您使用哪种Content-Type发送响应?因为我怀疑它根本没有被解析为XML.在这种情况下,jQuery将向您传递文档的字符串,而不是XML DOM.

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.

然后,当您调用"$(xml)"时,它将根据该字符串(*)来创建文档内容,该字符串被解析为HTML,而不是XML.在HTML中,没有CDATA部分,因此浏览器可能会丢弃它们,或将其视为注释.

Then when you call "$(xml)", it will be creating document content from that string(*) — parsed as HTML, not XML. In HTML there is no such thing as a CDATA section, so browsers might discard them, or treat them as comments.

我怀疑这是因为,当文档为XML时,"project.html()"实际上不起作用. "html()"仅返回与标准"innerHTML"属性(**)相同的属性,该属性仅适用于HTML文档;在XML元素上未定义.

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.

将输出包含在CDATA标记中,使我们可以相对放心地通过API发送回有效数据.

Enclosing the output in CDATA tags allows us to rest relatively assured that we're sending back valid data via our API.

好吧,相对":如果您的数据碰巧包含]]>",您仍然会丢失. <![CDATA [部分旨在用作提高手工创作可写性的拐杖;机器生成的XML实际上应该仅以正常方式使用实体编码.通常,服务器应用程序应使用适当的XML工具生成响应,在这种情况下,响应将自动完成.

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.

(**:我从来不了解jQuery何时需要将文档片段创建和CSS选择压缩到同一函数中.它们是完全不同的操作,您不希望混淆,就像这里可能发生的那样. )

(*: 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自定义属性.不幸的是,由于正则表达式无法解析HTML,它将很高兴地过滤掉文本中看起来像HTML属性的有效部分.不是jQuery的漂亮部分之一.)

(**: 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使用CDATA解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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