2011-12-05 109 views
1

我正在尋找支持名稱空間xml的AJAX JavaScript庫。支持名稱空間xml的AJAX庫

我讀了幾十個帖子(包括在stackoverflow上),但沒有找到一個好的答案。有許多AJAX示例,但只要名稱空間發揮作用就會中斷(例如使用jQuery選擇器就是這種情況)。

+0

您能否提供一些示例? – reconbot

+0

呃...打破的例子?使用jQuery:$(responseXML).find(「foo:bar」) – Christophe

+0

$(「nspace \\:data」,xml).find(「foo \\:bar」)。each(function(i){' http://stackoverflow.com/questions/853740/jquery-xml-parsing-with-namespaces –

回答

0

我不確切知道你在調用「AJAX JavaScript庫」 - 發出HTTP請求是一個問題不同於訪問文檔樹中的節點的

如果你瞭解圖書館爲「用於開發軟件資源的集合」(Wikipedia),那麼,作爲JSX庫的一部分,我已經寫了相當compatible¹包裝爲XMLHttpRequest和命名空間的DOM Level 3 XPathhttp.jsxpath.js

http.js支持同步和異步處理,甚至可以訪問本地文件系統(如果授予權限)。因爲JSX有資格作爲庫,那麼可以分別使用http.jsxpath.js,並使用外部代碼來補充其中一個,或者一起使用。

例如,您可以一起使用它們,如下所示。假設您有相關的資源名test.xml

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soap:Body> 
    <res:Response 
     xmlns:res="http://domain.example/Response"> 
     <res:OUTPUT> 
     <res:UNDO_COUNT>1.0</res:UNDO_COUNT> 
     <res:MSG>Undo complete (No more to undo)</res:MSG> 
     </res:OUTPUT> 
    </res:Response> 
    <res:OUTPUT 
     xmlns:res="http://domain.example/Response"> 
     foo 
    </res:OUTPUT> 
    </soap:Body> 
</soap:Envelope> 

一個XML文檔,你想從它的res:UNDO_COUNT元素提取1.0,你可以寫:

<!-- 1. Include prerequisites and dependencies using Resource Builder (recommended) --> 
<script type="text/javascript" src="builder.php?src=object,string,http,xpath"></script> 

<script type="text/javascript"> 
    /* 
    * 2. Construct the HTTP request wrapper; 
    * the default is a GET request with asynchronous handling 
    */ 
    var request = new jsx.net.http.Request("test.xml"); 

    /* 3. Prepare processing of the HTTP response */ 
    request.setSuccessListener(function (response) { 
    /* 5. Get the reference to the XMLDocument object */ 
    var doc = response.responseXML; 

    /* 6. Create the namespace resolver that fits your query best */ 
    var nsResolver = jsx.xpath.createFullNSResolver(null, doc); 

    /* 7. Make the XPath query */ 
    var nodes = jsx.xpath.evaluate("//res:UNDO_COUNT/text()", doc, nsResolver); 

    /* 
    * 8. Process the result. jsx.xpath.evaluate() returns a reference 
    * to an Array instance if you do not specify the result type. 
    */ 

    /* "1.0" */ 
    console.log(nodes[0].data); 
    }); 

    /* 4. Make the HTTP request */ 
    request.send(); 
</script> 

參見:Parsing XML/RSS from URL using Java Script

¹ JSX:object.js,http.jsxpath.js的組合已在基於Gecko,WebCore,MSHTML和Opera瀏覽器中測試過。但是,JSX現在是大部分是實驗性的代碼。

Testcase,看劇本控制檯

建設性的反饋意見是值得歡迎的。另外,JSX是free software。 (您目前還不能做checkout,但我正在努力。)

+0

我對請求和響應處理都感興趣(我的主要問題是後者)。感謝你的回覆,我會看看你的包裝。你在Internet Explorer 7/8/9中測試過它嗎?對於最後一個鏈接,只要命名空間起作用,我恐怕沒有任何相關的東西。 – Christophe

+0

我無法測試IE7中的xpath.js(今天提交)的HEAD版本。 Wine上的.2800.1106,所以支持MSXML的'selectNodes',並且我不期望在後來的IE版本(相同的DOM)中出現意外。至於HTTP請求,請看導入JSX:http.js(請參閱編輯)。這已經在指定的瀏覽器中進行了測試,至少包括IE 7和8,並且正在爲商業網站提供支持(當然,那些版本的JSX修訂版本稍微老舊)。 – PointedEars

+0

@Christophe你是什麼意思 - 沒有關係? RSS/XML使用名稱空間。您需要一個名稱空間解析器來查找具有XPath的元素。我之前的文章顯示瞭如何製作和使用一個。 'jsx.xpath.create ... NSResolver()'構建它。 – PointedEars