简体   繁体   中英

Find xml attribute values in a SOAP response with javascript

I'm working on an outlook web add-in, I use GetItem 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 Find xml attribute values with javascript without success. I'm not sure I'm able to use other external libraries since this is hosted in an office365 environment.

This is what I have been trying to do

var responseXML = 
'<?xml version="1.0" encoding="utf-8"?>'+
' <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">'+
'   <s:Header>'+
'       <h:ServerVersionInfo MajorVersion="15" MinorVersion="20" MajorBuildNumber="3955" MinorBuildNumber="27" Version="V2018_01_08" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>'+
'   </s:Header>'+
'   <s:Body>'+
'       <m:GetItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">'+
'           <m:ResponseMessages>'+
'               <m:GetItemResponseMessage ResponseClass="Success">'+
'                   <m:ResponseCode>NoError</m:ResponseCode>'+
'                   <m:Items>'+
'                       <t:Message>'+
'                           <t:ItemId Id="AAMkADZh" ChangeKey="CQABA"/>'+
'                           <t:Subject>the email subject</t:Subject>'+
'                       </t:Message>'+
'                   </m:Items>'+
'               </m:GetItemResponseMessage>'+
'           </m:ResponseMessages>'+
'       </m:GetItemResponse>'+
'   </s:Body>'+
'</s:Envelope>';

xmlDoc = $.parseXML(responseXML),
$xml = $(xmlDoc),
$itemId = $xml.find("ItemId");
$itemId.each(function(index, element) { 
    if(element.attributes["ChangeKey"]) {
        console.log("ChangeKey :" + element.attributes["ChangeKey"].value);
    }
});

but $itemId does not return a collection, although I'm expecting only one item, i think it doesn't hurt to leave the iteration prepared.

https://jsbin.com/matagujeye/1/edit?js,console

I found out here that I should include the name space and double escape the colons.

So the following code works:

var responseXML = 
'<?xml version="1.0" encoding="utf-8"?>'+
' <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">'+
'   <s:Header>'+
'       <h:ServerVersionInfo MajorVersion="15" MinorVersion="20" MajorBuildNumber="3955" MinorBuildNumber="27" Version="V2018_01_08" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>'+
'   </s:Header>'+
'   <s:Body>'+
'       <m:GetItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">'+
'           <m:ResponseMessages>'+
'               <m:GetItemResponseMessage ResponseClass="Success">'+
'                   <m:ResponseCode>NoError</m:ResponseCode>'+
'                   <m:Items>'+
'                       <t:Message>'+
'                           <t:ItemId Id="AAMkADZh" ChangeKey="CQABA"/>'+
'                           <t:Subject>the email subject</t:Subject>'+
'                       </t:Message>'+
'                   </m:Items>'+
'               </m:GetItemResponseMessage>'+
'           </m:ResponseMessages>'+
'       </m:GetItemResponse>'+
'   </s:Body>'+
'</s:Envelope>';

xmlDoc = $.parseXML(responseXML),
$xml = $(xmlDoc),
$itemId = $xml.find("t\\:ItemId");
$itemId.each(function(index, element) { 
    if(element.attributes["ChangeKey"]) {
        console.log("ChangeKey :" + element.attributes["ChangeKey"].value);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM