2014-12-04 161 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友好。