title: xmlbuilder2 and NodeJS/ExpressJS
body: <p>I have a requirement to provide an endpoint that generates a sitemap.</p>
  <p>This endpoint uses 'xmlbuilder2' and the goal is to return pure XML that can be delivered to the browser.</p>
  <pre><code>  @Get()
    public getSitemapFile(): Promise&lt;any&gt; {
      return new Promise&lt;any&gt;((resolve, reject) =&gt; {
        const root: SiteMapRoot = {
          xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9'
        };
        const urlset = create().ele(root.xmlns, 'urlset');
  
        // urlset.com('Testing things out -- building out items');
        const newUrl: SiteMapDetail = {
          loc: 'https://testsitemapurl.com/',
          lastmod: new Date(),
          changefreq: 'daily'
        };
  
        urlset
          .ele('url')
            .ele('loc')
              .txt(newUrl.loc).up()
            .ele('lastmod')
              .txt(newUrl.lastmod.toISOString()).up()
            .ele('changefreq')
              .txt(newUrl.changefreq).up();
  
        const resultXml = urlset.end({ prettyPrint: true });
        console.log(resultXml);
  
        this.setHeader('Content-Type', 'application/xml');
        resolve(urlset.toString());
      });
    }
  </code></pre>
  <p>The expected response would be something like this:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;urlset xmlns=&quot;http://www.sitemaps.org/schemas/sitemap/0.9&quot;&gt;
    &lt;url&gt;
      &lt;loc&gt;https://testsitemapurl.com/&lt;/loc&gt;
      &lt;lastmod&gt;2021-11-23T19:15:10.834Z&lt;/lastmod&gt;
      &lt;changefreq&gt;daily&lt;/changefreq&gt;
    &lt;/url&gt;
  &lt;/urlset&gt;
  </code></pre>
  <p>However, receiving the following as the response:</p>
  <pre><code>&quot;&lt;urlset xmlns=\&quot;http://www.sitemaps.org/schemas/sitemap/0.9\&quot;&gt;
  &lt;url&gt;
      &lt;loc&gt;https://testsitemapurl.com/&lt;/loc&gt;
      &lt;lastmod&gt;2021-11-23T19:15:10.834Z&lt;/lastmod&gt;
      &lt;changefreq&gt;daily&lt;/changefreq&gt;
  &lt;/url&gt;
  &lt;/urlset&gt;&quot;
  </code></pre>
  <p>What is tripping me up is:</p>
  <ul>
  <li>The return type is set to XML</li>
  <li>The return of the XML object is string
  <ul>
  <li>This explains why I receive the XML string value in quotes as the response</li>
  </ul>
  </li>
  </ul>
  <p>Is there something I am doing wrong (obviously there is)? Is there a different utility I can use?</p>