2

Ajax.Request我已经从in 中返回了 XML Ajax.Response.responseXML。我的 XML 实际上包含我希望附加到我的文档中的 XHTML 标记。

但是,我在将节点附加responseXML到当前文档中时遇到问题。我怀疑这是因为它们是 XML 节点而不是 DOM 节点。

包装responseXMLin$()似乎也不会返回节点的扩展版本,并且文档没有说它会或不会。

Prototype 是否提供了一种使用 的方法responseXML,如果没有,是否有跨浏览器的方法来解析响应并使用它?

4

2 回答 2

2

尽管我接受了 Christian 的答案作为“正确”答案,但我的一个同事向我展示了一种使用 Prototype 解析 XML 响应的方法。

而不是使用responseXML,您只需创建一个Element,然后update()使用responseText. 然后,您可以使用select()类似的方法来遍历节点。

于 2010-03-02T06:47:45.807 回答
1

没有原型没有解析 XML 的本机方式。您也不能使用 $(xml) 扩展 xml,然后使用 .next()、.select() 等遍历 DOM。

这是我最近在一个项目中从拼写检查结果中手动解析一些 XML 的示例。应该让你开始。

parseResults: function(results) {
var c = results.responseXML.getElementsByTagName('c');
var corrections = $A(c);
if(!corrections.size()){
  this.link.insert({ after: '<span class="spellCheckNoErrors">No spelling errors</span>' });
  (function(){ this.link.next().remove(); }.bind(this)).delay(1);
  return null;
}
this.res = $A();
corrections.each(function(node){
  sugg = node.childNodes[0].nodeValue;
  offset = node.attributes[0].nodeValue;
  len = node.attributes[1].nodeValue;
        this.res.push(
    $H({
      word: this.text.substr(offset, len),
      suggestions: sugg.split(/\s/)
          })
  );
},this);
this.overlay();
},
于 2010-02-25T18:40:52.577 回答