title: get responseXML when responseType === 'arraybuffer' after load
body: <p>I am using this code to retrieve imagery data and it works.</p>
  
  <pre><code>        function imageLoadFunction (image, src) {
          var img = image.getImage();
          if (typeof window.btoa == 'function') {
              var xhr = new XMLHttpRequest();
              xhr.open('GET', 'https://cors-anywhere.herokuapp.com/' + src, true);
              xhr.setRequestHeader("Access-Control-Allow-Headers", "origin,x-requested-with");
              xhr.responseType = 'arraybuffer'; // SHOULD BE 'arraybuffer'
              xhr.onload = function(e) {
                 if (this.status == 200) {
                     var uInt8Array = new Uint8Array(this.response);
                     var i = uInt8Array.length;
                     var binaryString = new Array(i);
                     while (i--) {
                         binaryString[i] = String.fromCharCode(uInt8Array[i]);
                     }
                     var data = binaryString.join('');
                     var type = xhr.getResponseHeader('content-type');
                     if (type.indexOf('image') === 0) {
                         img.src = 'data:' + type + ';base64,' + window.btoa(data);
                     }
                 }
              };
              xhr.send();
          }
      };
  </code></pre>
  
  <p>I'd like to catch Exceptions from the (WMS) server when errors occurs, and for this I need to use <code>responseXML</code>. However, there seems to be two problems:</p>
  
  <ol>
  <li>the <code>responseType</code> is <code>'arraybuffer</code> and thus <code>responseXML</code> says it sdhould be <code>'' or 'document'</code></li>
  <li>If I try to change the <code>responseType</code> I receive error that I can't if request is <code>LOADED or DONE</code>.</li>
  </ol>
  
  <p>Is there a way I can still use <code>responseType === 'arraybuffer'</code> and <code>responseXML</code> together?</p>
  
  <p>Basically, I would need to implement the code like so (only relevant part):</p>
  
  <pre><code>if (type.indexOf('image') === 0) {
     img.src = 'data:' + type + ';base64,' + window.btoa(data);
  } else {
     xmlDoc = xhr.responseXML;
     txt = "";
     x = xmlDoc.getElementsByTagName("ServiceException");
     for (i = 0; i &lt; x.length; i++) {
         txt += x[i].childNodes[0].nodeValue + "&lt;br&gt;";
     }
     console.error(txt);
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Ok so the trick was to use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode" rel="nofollow noreferrer"><code>String.fromCharCode()</code></a></p>
  
  <pre><code>if (type.indexOf('image') === 0) {
     img.src = 'data:' + type + ';base64,' + window.btoa(data);
  } else {
     var txt = String.fromCharCode.apply(null, uInt8Array);
     console.error(txt);
  }
  </code></pre>
  
  <p>I know it's not yet an XML but now it's much easier to treat the Exceptions.</p>