2

In my database I have some objects that need to be represented in xml file. What is the simplest/easiest way to convert the item objects into a xml representation of the items? What python library should I use?

<items>
    <item>
        <picture><![CDATA[foo.jpg]]></picture>
        <title><![CDATA[Foo]]></title>
        <link><![CDATA[http://www.foo.com]]></link>
    <color><![CDATA[red]]></color>
    </item>

    <item>
        <picture><![CDATA[baz.jpg]]></picture>
        <title><![CDATA[Baz]]></title>
        <link><![CDATA[http://www.baz.com]]></link>
    <color><![CDATA[blue]]></color>
    </item>
</items>
Seitaridis
  • 4,459
  • 9
  • 53
  • 85
  • Related: [How to convert XSD to Python Class](http://stackoverflow.com/questions/1072853/) – Piotr Dobrogost Oct 15 '12 at 20:13
  • Possible duplicate of [How can I change a Python object into XML?](https://stackoverflow.com/questions/3334446/how-can-i-change-a-python-object-into-xml) – Stevoisiak Feb 13 '18 at 20:56

3 Answers3

4

How important is it to have that exact structure? Django includes a serialization framework that can convert querysets to XML, but it doesn't match your format at all.

Otherwise you'll need to write it manually - for this I find it's easiest to write a model method that can output a single instance in the desired format, then call it on each member of the queryset in a loop.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • The Django project I'm working on uses version 1.0.2 of Django, so I'm afraid it won't be possible to use the serialization framework. I'll have to find a python library or work on a custom implementation. – Seitaridis Jan 21 '11 at 11:02
  • 3
    Why not? Serialization has been in Django since long before 1.0. [Here](http://docs.djangoproject.com/en/1.0/topics/serialization/#topics-serialization) are the 1.0 serialization docs, which don't differ much from the 1.2 ones. – Daniel Roseman Jan 21 '11 at 11:13
  • +1 for directing Seitaridis to the concerned docs. Real Cool. – sidhshar Jan 21 '11 at 11:22
  • Excuse me. In the link for serialization framework I saw "This document describes Django 1.2." and I believed that it's a new feature, that comes with Django 1.2. I will give it a try – Seitaridis Jan 21 '11 at 11:23
  • the link provided is broke – James Oravec Apr 27 '15 at 23:51
4

If its really that simple, and you have django available (I see your tag), use a Django template!

There's some examples on the django docs and in the blogosphere as a whole. If this is being sent as a response then you also have to set the HttpResponse header. Pretty standard stuff!

Django template doc:

http://docs.djangoproject.com/en/1.2/topics/templates/

For your case, create a template like this, and pass it a queryset, 'q', and then:

<xml>
<items>
   {% for p in q %}
    <item>
        <picture><![CDATA[{{p.filename}}]]></picture>
        <title><![CDATA[{{p.title}}]]></title>
        <link><![CDATA[{{p.link}}]]></link>
    <color><![CDATA[{{p.color}}]]></color>
    </item>
   {% endfor %}
</items>

Assuming q is a queryset on a Model with those fields (filename, title, link, color).

Spacedman
  • 92,590
  • 12
  • 140
  • 224
4

Use the string.Template class.

import string
item_template = string.Template( """<item>
    <picture><![CDATA[$a]]></picture>
    <title><![CDATA[$b]]></title>
    <link><![CDATA[$c]]></link>
<color><![CDATA[$d]]></color>
</item>""")
item = item_template( a="foo.jpg", b="Foo", c="http://www.foo.com", d="red" )

You should be able to then build a list of items as follows:

item_list= string.Template( "<items>$item_list</items>" )
item_list.substitute( item_list= "\n".join( some_list_of_item_strings ) )

That kind of thing can build up proper XML documents from non-XML pieces.

Andreas Bergström
  • 13,891
  • 5
  • 59
  • 53
S.Lott
  • 384,516
  • 81
  • 508
  • 779