title: Using XMLWriter to generate XML File in PHP - outputMemory() function not working
body: <p>I am trying to create and write an <strong>XML</strong> file in PHP using <strong>XMLWriter</strong>.</p>
  
  <p>My code is as follows:</p>
  
  <pre><code>$writer = new XMLWriter();
  $writer-&gt;openURI('php://output');
  $writer-&gt;startDocument('1.0','UTF-8');
  $writer-&gt;setIndent(4);
  $writer-&gt;startElement('items');
  $writer-&gt;startElement("main");
  $writer-&gt;writeElement('user_id', 3);
  $writer-&gt;writeElement('msg_count', 11);
  $writer-&gt;endElement();
  $writer-&gt;startElement("msg");
  $writer-&gt;writeAttribute('category', 'test');
  $writer-&gt;endElement();
  $writer-&gt;endElement();
  $writer-&gt;endDocument();
  $writer-&gt;flush();
  
  header('Content-type: text/xml');
  
  echo $writer-&gt;outputMemory();
  </code></pre>
  
  <p>This seems to create valid XML onscreen </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;items&gt;
    &lt;main&gt;
      &lt;user_id&gt;3&lt;/user_id&gt;
      &lt;msg_count&gt;11&lt;/msg_count&gt;
    &lt;/main&gt;
    &lt;msg category="test"/&gt;
  &lt;/items&gt;
  </code></pre>
  
  <p>However when I try to echo the XML using:</p>
  
  <pre><code>echo $writer-&gt;outputMemory();
  </code></pre>
  
  <p>The output is blank, additionally when the XML file is created using:</p>
  
  <pre><code>$filename = "xml/example.xml";
  $file = $writer-&gt;outputMemory();
  file_put_contents($filename,$file);
  </code></pre>
  
  <p>The file contents are also blank.</p>
  
  <p>How I am able to output the XML to the file? The <code>outputMemory()</code> function doesn't seem to be getting populated.</p>