A better XML Parser for Python 3 with nested classes (dicttoxml)

676 Views Asked by At

This is really part 2 to this question: A better XML Serializer for Python 3 In that question, I didn't specify that I might have the classes nested. The picklers and marshallers didn't produce the XML I was looking for.

import sys
from dicttoxml import dicttoxml
import enhancedminidom
from xml.dom.minidom import parseString

class Person:
    def __init__(self, _firstname, _lastname, _address):
        self.firstName = _firstname
        self.lastName = _lastname
        self.homeAddress = _address
class Address:
    def __init__(self, _city, _state):
        self.city = _city
        self.state = _state

address1 = Address("Dallas", "TX")
person1 = Person("John", "Doe", address1)
personDict = vars(person1) # vars is pythonic way of converting to dictionary
xml = dicttoxml(personDict, attr_type=False, custom_root='Person') # set root node to Person
print(xml)
dom = parseString(xml)
xmlFormatted = dom.toprettyxml()
print(xmlFormatted)

Desired XML:

<person> 
   <firstname>John</firstname>
   <lastname>Doe</lastname>
   <homeAddress>
        <city>Dallas</city>
        <state>TX</state>
   </homeAddress>
</person>

Error I'm getting in the dicttoxml function:

Traceback (most recent call last):
  File "E:/GitHub/NealWalters/PythonEDI/SerializeTest.py", line 19, in <module>
    xml = dicttoxml(personDict, attr_type=False, custom_root='Person') # set root node to Person
  File "E:\Python\Python36\lib\site-packages\dicttoxml.py", line 393, in dicttoxml
    convert(obj, ids, attr_type, item_func, cdata, parent=custom_root), 
  File "E:\Python\Python36\lib\site-packages\dicttoxml.py", line 189, in convert
    return convert_dict(obj, ids, parent, attr_type, item_func, cdata)
  File "E:\Python\Python36\lib\site-packages\dicttoxml.py", line 251, in convert_dict
    val, type(val).__name__)
TypeError: Unsupported data type: <__main__.Address object at 0x000001C94062B0B8> (Address)

Process finished with exit code 1
1

There are 1 best solutions below

0
On BEST ANSWER

Adding the Address class to the Person class means that Vars() can no longer convert class to a key pair dictionary. As a work around you can import json. Convert the object to json string and load the string as a json object.

from dicttoxml import dicttoxml
import json

class Person:
    def __init__(self, _firstname, _lastname, _address):
        self.firstName = _firstname
        self.lastName = _lastname
        self.homeAddress = _address
class Address:
    def __init__(self, _city, _state):
        self.city = _city
        self.state = _state

address1 = Address("Dallas", "TX")
person1 = Person("John", "Doe", address1)
obj = json.loads(json.dumps(person1, default=lambda x: x.__dict__))

xml = dicttoxml(obj, attr_type=False, custom_root='Person'