title: Writing XML files with Python
body: <p>I have created an XML document with the following contents.</p>
  
  <pre><code>&lt;books&gt;
    &lt;book id="1"&gt;
      &lt;title&gt;Title01&lt;/title&gt;
      &lt;authors/&gt;
      &lt;pages&gt;
        &lt;page&gt;Page01&lt;/page&gt;
        &lt;page&gt;Page02&lt;/page&gt;
        &lt;page&gt;Page03&lt;/page&gt;
        &lt;page&gt;Page04&lt;/page&gt;
        &lt;page&gt;Page05&lt;/page&gt;
      &lt;/pages&gt;
    &lt;/book&gt;
    &lt;book id="2"&gt;
      &lt;title&gt;Title02&lt;/title&gt;
      &lt;authors/&gt;
      &lt;pages&gt;
        &lt;page&gt;Page01&lt;/page&gt;
        &lt;page&gt;Page02&lt;/page&gt;
        &lt;page&gt;Page03&lt;/page&gt;
        &lt;page&gt;Page04&lt;/page&gt;
        &lt;page&gt;Page05&lt;/page&gt;
      &lt;/pages&gt;
    &lt;/book&gt;
  &lt;/books&gt;
  </code></pre>
  
  <p>I then use a Python script to split up and write the individual books into separate files;however, the resulting files are not XML files because they do not have the XML declaration. Is there a way of creating XML files in Python? </p>
  
  <p>The idea is to ensure that each file has the XML declaration as show below.</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;
    &lt;book id="1"&gt;
      &lt;title&gt;Title01&lt;/title&gt;
      &lt;authors/&gt;
      &lt;pages&gt;
        &lt;page&gt;Page01&lt;/page&gt;
        &lt;page&gt;Page02&lt;/page&gt;
        &lt;page&gt;Page03&lt;/page&gt;
        &lt;page&gt;Page04&lt;/page&gt;
        &lt;page&gt;Page05&lt;/page&gt;
      &lt;/pages&gt;
    &lt;/book&gt;
  </code></pre>