6

I am looking to convert a Python object into XML data. I've tried lxml, but eventually had to write custom code for saving my object as xml which isn't perfect.

I'm looking for something more like pyxser. Unfortunately pyxser xml code looks different from what I need.

For instance I have my own class Person

Class Person:
    name = ""
    age = 0
    ids = []

and I want to covert it into xml code looking like

<Person>
    <name>Mike</name>
    <age> 25 </age>
    <ids>
        <id>1234</id>
        <id>333333</id>
        <id>999494</id>
   </ids>
</Person>

I didn't find any method in lxml.objectify that takes object and returns xml code.

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
mdob
  • 2,224
  • 3
  • 22
  • 25
  • 1
    possible duplicate of [serializing python objects to XML](http://stackoverflow.com/questions/2101902/serializing-python-objects-to-xml) – Mark Byers Jul 26 '10 at 11:48
  • Related: [How can I convert XML into a Python object?](https://stackoverflow.com/q/418497/3357935) – Stevoisiak Feb 13 '18 at 20:54

2 Answers2

2

Best is rather subjective and I'm not sure it's possible to say what's best without knowing more about your requirements. However Gnosis has previously been recommended for serializing Python objects to XML so you might want to start with that.

From the Gnosis homepage:

Gnosis Utils contains several Python modules for XML processing, plus other generally useful tools:

  • xml.pickle (serializes objects to/from XML)
  • API compatible with the standard pickle module)
  • xml.objectify (turns arbitrary XML documents into Python objects)
  • xml.validity (enforces XML validity constraints via DTD or Schema)
  • xml.indexer (full text indexing/searching)
  • many more...

Another option is lxml.objectify.

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 2
    Can you add an example of a class being serialized? – Stevoisiak Feb 13 '18 at 15:09
  • 1
    For any recent readers, gnosis is *long* out of date. I couldn't even install it without messing with its `setup.py`, and then it failed to parse certain things, looking for object properties that no longer exist in the language. – Pavel Komarov Mar 19 '18 at 17:25
  • @PavelKomarov so what's the up to date library of choice then? I just want to save and load a python object, unfortunately I have to use xml. Why is this so difficult? I've been googling around and there doesn't seem to be a "3 lines or less" solution – Finni May 18 '21 at 11:19
  • I ended up coding my own recursive solution to walk the object structure, making use of Python's reflection. – Pavel Komarov May 19 '21 at 12:49
-5

Mike,

you can either implement object rendering into XML :

class Person:
  ...
  def toXml( self):
    print '<Person>'
    print '\t<name>...</name>
    ...
    print '</Person>'

or you can transform Gnosis or pyxser output using XSLT.

nad2000
  • 4,526
  • 1
  • 30
  • 25
  • 2
    Printing text equivalent to XML is not the same as serializing as XML. – Stevoisiak Feb 13 '18 at 15:05
  • 1
    And then you serialize a field that contains an < or an & or any other XML special character and everything falls apart. Don't even start going down that path - you never see the end of it. – Algoman Mar 19 '21 at 12:44