How can I generate custom XML for a SOAP client? Im building a SOAP client that consumes from a SOAP web services in a Drupal 8 module.

I need to generate this final XML:

$inpunt_data = '&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:qual="http://websoap.com/"&gt;

                           &lt;soapenv:Header/&gt;

                           &lt;soapenv:Body&gt;

                              &lt;qual:myMethod&gt;

                                 &lt;!--Optional:--&gt;

                                 &lt;qual:xmlMethod&gt;&lt;![CDATA[

                                &lt;Tag&gt;
                                &lt;/Tag&gt;
                               &lt;/qual:Method&gt;';


How do I generate soapenv headers?

I'm using simplexml:

$getXML = new SimpleXMLElement($input_data);  
             $urlTarifa = 'http://soapurl.com?wsdl';
             $client = new SoapClient($urlTarifa);
This is how I've done it:

some-feed.html.twig

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"&gt;
  &lt;channel&gt;
    &lt;atom:link href="{{ some_variable_set_in_preprocess }}" rel="self" type="application/rss+xml"/&gt;
      /* -- rest of feed omitted */
  &lt;/channel&gt;
&lt;/rss&gt;


Then hook_theme() to register the template:

function hook_theme() {
  return [
    'some_feed' =&gt; [
      'render element' =&gt; 'elements',
      'template' =&gt; 'some-feed',
    ],
  ];
}


You can then generate a response with that theme:

$build = [
  '#theme' =&gt; 'some_feed',
];

$response = new \Symfony\Component\HttpFoundation\Response();
$xml = \Drupal::service('renderer')-&gt;renderRoot($build);
$response-&gt;setContent($xml);
$response-&gt;headers-&gt;set('Content-Type', 'text/xml');


You now have a Response object to work with. Depending on where you are working, you can return that, or set it as a response.