title: Find xml attribute values in a SOAP response with javascript
body: <p>I'm working on an outlook web add-in, I use <a href="https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/getitem-operation-email-message" rel="nofollow noreferrer">GetItem</a> to get the ChangeKey from the Exchange Web Service SOAP response, a value I need to compose further requests. The problem is that so far I haven't been able to get that. I have followed <a href="https://stackoverflow.com/questions/11624448/find-xml-attribute-values-with-javascript">Find xml attribute values with javascript</a> without success.
  I'm not sure I'm able to use other external libraries since this is hosted in an office365 environment.</p>
  <p>This is what I have been trying to do</p>
  <pre><code>var responseXML = 
  '&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;'+
  ' &lt;s:Envelope xmlns:s=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;'+
  '   &lt;s:Header&gt;'+
  '       &lt;h:ServerVersionInfo MajorVersion=&quot;15&quot; MinorVersion=&quot;20&quot; MajorBuildNumber=&quot;3955&quot; MinorBuildNumber=&quot;27&quot; Version=&quot;V2018_01_08&quot; xmlns:h=&quot;http://schemas.microsoft.com/exchange/services/2006/types&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;/&gt;'+
  '   &lt;/s:Header&gt;'+
  '   &lt;s:Body&gt;'+
  '       &lt;m:GetItemResponse xmlns:m=&quot;http://schemas.microsoft.com/exchange/services/2006/messages&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:t=&quot;http://schemas.microsoft.com/exchange/services/2006/types&quot;&gt;'+
  '           &lt;m:ResponseMessages&gt;'+
  '               &lt;m:GetItemResponseMessage ResponseClass=&quot;Success&quot;&gt;'+
  '                   &lt;m:ResponseCode&gt;NoError&lt;/m:ResponseCode&gt;'+
  '                   &lt;m:Items&gt;'+
  '                       &lt;t:Message&gt;'+
  '                           &lt;t:ItemId Id=&quot;AAMkADZh&quot; ChangeKey=&quot;CQABA&quot;/&gt;'+
  '                           &lt;t:Subject&gt;the email subject&lt;/t:Subject&gt;'+
  '                       &lt;/t:Message&gt;'+
  '                   &lt;/m:Items&gt;'+
  '               &lt;/m:GetItemResponseMessage&gt;'+
  '           &lt;/m:ResponseMessages&gt;'+
  '       &lt;/m:GetItemResponse&gt;'+
  '   &lt;/s:Body&gt;'+
  '&lt;/s:Envelope&gt;';
  
  xmlDoc = $.parseXML(responseXML),
  $xml = $(xmlDoc),
  $itemId = $xml.find(&quot;ItemId&quot;);
  $itemId.each(function(index, element) { 
      if(element.attributes[&quot;ChangeKey&quot;]) {
          console.log(&quot;ChangeKey :&quot; + element.attributes[&quot;ChangeKey&quot;].value);
      }
  });
  </code></pre>
  <p>but <code>$itemId</code> does not return a collection, although I'm expecting only one item, i think it doesn't hurt to leave the iteration prepared.</p>
  <p><a href="https://jsbin.com/matagujeye/1/edit?js,console" rel="nofollow noreferrer">https://jsbin.com/matagujeye/1/edit?js,console</a></p>