Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert python object to XML representation

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>
like image 290
Seitaridis Avatar asked Dec 19 '25 17:12

Seitaridis


1 Answers

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.

like image 162
Daniel Roseman Avatar answered Dec 21 '25 08:12

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!