title: How to get SOAP body with node.js?
body: <p>I have incoming SOAP message in a format like this:</p>
  
  <pre><code>const soap = `
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"&gt;
      &lt;env:Header/&gt;
      &lt;env:Body property="def"&gt;
          &lt;Root&gt;
              &lt;Abc attr="abc"&gt;123456&lt;/Abc&gt;
          &lt;/Root&gt;
      &lt;/env:Body&gt;
  &lt;/env:Envelope&gt;
  `;
  </code></pre>
  
  <p>What I expect to get is the content of <code>&lt;env:Body&gt;</code> unchanged:</p>
  
  <pre><code>&lt;Root&gt;
      &lt;Abc attr="abc"&gt;123456&lt;/Abc&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>To extract the body I use simple regex:</p>
  
  <pre><code>function getSoapBody(xmlStr) {
      let soapBody = null;
      if (xmlStr) {
          const soapBodyRegex = /&lt;env:Body&gt;([\s\S]*)&lt;\/env:Body&gt;/im;
          const soapBodyRegexMatchResult = xmlStr.match(soapBodyRegex);
          soapBody = soapBodyRegexMatchResult[1];
      }
      return soapBody;
  }
  </code></pre>
  
  <p>However, getting body with regex is not ideal of course. </p>
  
  <p>Moreover, I don't want to reinvent the wheel and I'm looking for a solution (or even npm package) that will basically getSoapBody regardless of prefix used (the regex above will fail if the prefix changes) or if there are optional attributes etc.</p>