Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1115969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:14:07+00:00 2026-05-17T03:14:07+00:00

I need to write model data ( CharField s only) to an XML file

  • 0

I need to write model data (CharFields 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:

<?xml version="1.0" encoding="UTF-8"?>
<textFields>
     <textField id="0" text="HELLO WORLD" />
     <textField id="1" text="HELLO EARTH" />
     ...
</textFields>

1. I am using a serializer to write the xml data from the model:

from django.core import serializers
data = serializers.serialize('xml', myModel.objects.filter(instanceIwantTowrite), fields=('fieldName'))

2. I then create file using core.files:

from django.core.files import File    
f = open('/path/to/new/dir/content.xml', 'w')
myfile = File(f)

3. Write File data and close:

myfile.write(data)

myfile.close()

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?

Note: In serializer I use filter on the model objects because using get for the model instance returns an object not iterable error. In fact I filter it twice to get a single instance, seems like there must be a better way.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T03:14:07+00:00Added an answer on May 17, 2026 at 3:14 am

    You have two possible solutions here:

    1.

    You can extend base django xml serializer(django.core.serializers.xml_serializer.Serializer) and modify it so it will return data in your structure. You could then run ex.

    YourSerializer('xml', myModel.objects.filter(instanceIwantTowrite), fields=('fieldName'))
    

    and it will output data in your structure.

    2.

    Write simple function that will render template with your data structure and return xml data in your format:

    Python code

    from django.template.loader import render_to_string
    
    def my_serialize(query_set):
        xml = render_to_string('xml_template.xml', {'query_set': query_set})
    
        return xml
    

    Template xml_template.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <textFields>
         {% for object in query_set %}
         <textField id="{{ object.pk }}" text="{{ object.my_field }}" />
         {% endfor %}
    </textFields>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Both my Rails model and controller code need to write files to the file
I need to write a system to generate HTML email from a data model
The application need write file's last modification date. void Dater(String DateFile) { File file
I need to write a script in Matlab, which will read some data from
I need to write some input data files for a python program, and I
I need to write to a text file using JavaScript. I have a machine
I need to write the output of the code I have to a file
I'm stuck with a query I need to write. Given the following model: public
Im building an app that will use a Core Data model. I pretty new
The steps I go through... Add new ADO.NET Entity Data Model > Generate from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.