2010-02-24 47 views
2

我從Ajax.RequestAjax.Response.responseXML中得到了XML。我的XML實際上包含了我希望添加到我的文檔中的XHTML標籤。

但是,我在將當前文檔中的節點從responseXML附加到當前文檔中時出現問題。我懷疑這是因爲它們是XML節點而不是DOM節點。

包裝responseXML$()似乎沒有返回節點的擴展版本,並且文檔沒有說它會或不會。

原型是否提供了一種方法來使用responseXML,如果沒有,是否有一個跨瀏覽器的方式來解析響應並使用它?

回答

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(); 
}, 
2

雖然我接受了基督教的答案爲「正確」的答案,我的運動課給我的方式解析與原型的XML響應。

相反,使用responseXML,您只需創建一個Element,然後update()responseText即可。然後您可以使用select()及其類似方法遍歷節點。