代码之家  ›  专栏  ›  技术社区  ›  Zack The Human Kunal

使用原型的$()分析responseXML

  •  2
  • Zack The Human Kunal  · 技术社区  · 15 年前

    我有XML从 Ajax.Request 在里面 Ajax.Response.responseXML . 我的XML实际上包含了我希望附加到我的文档中的XHTML标记。

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

    包裹 响应性 在里面 $() 似乎也没有返回节点的扩展版本,文档也没有说明它会或不会返回。

    原型是否提供了利用 响应性 如果没有,是否有跨浏览器的方法来解析响应并使用它?

    2 回复  |  直到 13 年前
        1
  •  1
  •   Minimul    15 年前

    没有原型没有分析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();
    },
    
        2
  •  2
  •   Zack The Human Kunal    15 年前

    虽然我接受基督教徒的回答为“正确的”答案,但我的一个大学同学告诉我 一种方式 解析带有原型的XML响应。

    而是使用 responseXML ,您只需创建一个 Element 然后 update() 它与 responseText . 然后你可以使用 select() 以及像它这样的方法来遍历节点。