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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With