2014-12-04 160 views
1

我試圖從下面的XML中選擇特定節點"effectiveintervalstart"的值(從底部開始一直粘貼到底)。在XML中選擇節點

這裏是我的代碼:

//the XML is returned into req.responseXML.xml 
var result = req.responseXML.xml.toString(); 
      var doc = new ActiveXObject("MSXML2.DOMDocument"); 
      doc.async = false; 
      doc.loadXML(result); 

      var arrayAnswers = []; 
      var arr = doc.selectNodes("//b:effectiveintervalstart"); 
      for (var i = 0, len = arr.length; i < len; i++) { 

       arrayAnswers[i] = arr.nextNode.text; 
      } 

      alert(arrayAnswers); 
      alert(arrayAnswers.length) 

但是到目前爲止,這是返回一個空數組來代替。

這是我的XML。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Body> 
    <RetrieveMultipleResponse xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
     <RetrieveMultipleResult xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts"> 
     <a:Entities> 
      <a:Entity> 
     <a:Attributes xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic"> 
      <a:KeyValuePairOfstringanyType> 
      <b:key>calendarid</b:key> 
      <b:value i:type="c:guid" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/">933d2b7d-0e7c-e211-a14f-78e7d1620f84</b:value> 
      </a:KeyValuePairOfstringanyType> 
      <a:KeyValuePairOfstringanyType> 
      <b:key>calendarrules</b:key> 
      <b:value i:type="a:EntityCollection"> 
       <a:Entities> 
       <a:Entity> 
        <a:Attributes> 
        <a:KeyValuePairOfstringanyType> 
         <b:key>calendarruleid</b:key> 
         <b:value i:type="c:guid" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/">b77b4621-3774-e411-80db-005056971aab</b:value> 
        </a:KeyValuePairOfstringanyType>       

        /* this node */ 
        <a:KeyValuePairOfstringstring> 
         <b:key>effectiveintervalstart</b:key> 
         <b:value>11/26/2014</b:value> 
        </a:KeyValuePairOfstringstring> 
        /* */ 

        <a:KeyValuePairOfstringstring> 
         <b:key>rank</b:key> 
         <b:value>0</b:value> 
        </a:KeyValuePairOfstringstring> 

非常感謝您的幫助,非常感謝。 。

+0

我猜你的錯誤在於var arr = doc.selectNodes(「// b:effectiveintervalstart」); 嘗試「/ Envelope/Body/RetrieveMultipleResponse/RetrieveMultipleResult/Attributes/KeyValuePairOfStringanyType/Value/Entities/Entity/Attributes/KeyValuePairOfstringstring /」 – WickedFan 2014-12-04 15:59:32

+0

您在哪裏看到節點'b:effectiveintervalstart',它不存在...節點'// a:KeyValuePairOfstringstring [b:key ='effectiveintervalstartdate']/b:value'確實存在。您可能需要查看Sdk.Soap.js庫以獲取靈感https://code.msdn.microsoft.com/SdkSoapjs-9b51b99a。 – 2014-12-04 16:59:06

回答

0

正如@ThijsKuipers提到的那樣,'MSXML.DOMDocument'不知道你引用的命名空間('a:'和'b:')。

你必須使你的DOM知道的命名空間的加入:

//From: https://code.msdn.microsoft.com/SdkSoapjs-9b51b99a/sourcecode?fileId=113716&pathId=1614657476 

var ns = { 
    "s": "http://schemas.xmlsoap.org/soap/envelope/", 
    "a": "http://schemas.microsoft.com/xrm/2011/Contracts", 
    "i": "http://www.w3.org/2001/XMLSchema-instance", 
    "b": "http://schemas.datacontract.org/2004/07/System.Collections.Generic", 
    "c": "http://www.w3.org/2001/XMLSchema", 
    "d": "http://schemas.microsoft.com/xrm/2011/Contracts/Services", 
    "e": "http://schemas.microsoft.com/2003/10/Serialization/", 
    "f": "http://schemas.microsoft.com/2003/10/Serialization/Arrays", 
    "g": "http://schemas.microsoft.com/crm/2011/Contracts", 
    "h": "http://schemas.microsoft.com/xrm/2011/Metadata", 
    "j": "http://schemas.microsoft.com/xrm/2011/Metadata/Query", 
    "k": "http://schemas.microsoft.com/xrm/2013/Metadata", 
    "l": "http://schemas.microsoft.com/xrm/2012/Contracts" 
}; 

var namespaces = []; 
for (var i in ns) 
{ 
    namespaces.push("xmlns:" + i + "='" + ns[i] + "'"); 
} 

doc.setProperty("SelectionNamespaces", namespaces.join(" ")); 

然後,你希望你可以查詢你的XML。

跨瀏覽器支持

在一個側面說明:您不應該使用新的ActiveXObject( 「MSXML2.DOMDocument」)所有

的Chrome,火狐,Safari瀏覽器進行, Spartan項目和其他任何僅支持HTML5的瀏覽器不瞭解「ActiveXObject」。 我該如何解析XML,你可能會問? 前面提到的SoapJs使用document.evaluate。詳情請參閱this answer

的OData

如果您通過任何手段可以重寫您查詢使用OData endpoint那麼它將/可以返回JSON回其在本質上,就是更加的JavaScript友好。