title: Generate XML file from model data
body: <p>I need to write model data (<code>CharField</code>s only) to an XML file to contain the data for a flash file.  I am new to this, and the process is a little unclear to me for doing this in django.  I am creating an xml file, and then writing the text data to the file (as is done with the csv module, but to xml).  A very simplified xml file should result for the flash file to read, ie: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;textFields&gt;
       &lt;textField id="0" text="HELLO WORLD" /&gt;
       &lt;textField id="1" text="HELLO EARTH" /&gt;
       ...
  &lt;/textFields&gt;
  </code></pre>
  
  <p>1. I am using a serializer to write the xml data from the model:</p>
  
  <pre><code>from django.core import serializers
  data = serializers.serialize('xml', myModel.objects.filter(instanceIwantTowrite), fields=('fieldName'))
  </code></pre>
  
  <p>2. I then create file using <code>core.files</code>:</p>
  
  <pre><code>from django.core.files import File    
  f = open('/path/to/new/dir/content.xml', 'w')
  myfile = File(f)
  </code></pre>
  
  <p>3. Write File data and close:  </p>
  
  <pre><code>myfile.write(data)
  
  myfile.close()
  </code></pre>
  
  <p>This works so far, although the xml output contains the fields for the object "django-objects" etc, and I will have to see if I can interpret this in ActionScript easily for the flash file.  I would prefer to define the xml field names manually like in the csv module. As I am new to django and python, I am wondering if there is an easier, simpler way to do this?  </p>
  
  <p><strong>Note:</strong> In serializer I use filter on the model objects because using <code>get</code> for the model instance returns an <code>object not iterable</code> error.  In fact I filter it twice to get a single instance, seems like there must be a better way.  </p>