Serialize Python Dictionary to Xml

Serialize Python dictionary to XML

http://code.activestate.com/recipes/415983/

http://sourceforge.net/projects/pyxser/

http://soapy.sourceforge.net/

http://www.ibm.com/developerworks/webservices/library/ws-pyth5/

http://gnosis.cx/publish/programming/xml_matters_1.txt

Converting a dict to XML with attributes

This is not supported by dicttoxml as of yet, though the issue has been open from a long time.
https://github.com/quandyfactory/dicttoxml/issues/27

Though if your needs are not that complex, you can try this simple serializer out.

https://gist.github.com/reimund/5435343/

found it here :- Serialize Python dictionary to XML

Marshmallow: How to serialize a dict or an object to XML?

As per the answer here. You could always just post process the json to xml using a library. Try dicttoxml

import json
from dicttoxml import dicttoxml
marshmallow_json_string = schema.dumps
marshmallow_dict = json.loads(marshmallow_json_string)
marshmallow_xml = dicttoxml(marshmallow_dict)

Serialize dictionary int, custom_class to XML [Python]

As you mentioned an external library is ok, use jsonpickle .

A better XML Serializer for Python 3

The reason the output is showing xml as a dictionary is most likely because the properties don't have a reference to the class. You should consider using self. and assigning values within a __init__ function.

class Person:
def __init__(self):
self.firstName = "John"
self.lastName = "Doe"

There are many ways to convert an object to XML. However try using the package dicttoxml. As the name suggest you'll need to convert object to a dictionary, which can be done using vars().

The full solution:

from dicttoxml import dicttoxml

class Person:
def __init__(self):
self.firstName = "John"
self.lastName = "Doe"

person = vars(Person()) # vars is pythonic way of converting to dictionary
xml = dicttoxml(person, attr_type=False, custom_root='Person') # set root node to Person
print(xml)

Output:

b'<?xml version="1.0" encoding="UTF-8" ?><Person><firstName>John</firstName><lastName>Doe</lastName></Person>'

If you want to format the XML nicely, then you can use the built in xml.dom.minidom.parseString library.

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

class Person:
def __init__(self):
self.firstName = "John"
self.lastName = "Doe"

person = vars(Person()) # vars is pythonic way of converting to dictionary
xml = dicttoxml(person, attr_type=False, custom_root='Person') # set root node to Person
print(xml)

dom = parseString(xml)
print(dom.toprettyxml())

Output:

<?xml version="1.0" ?>
<Person>
<firstName>John</firstName>
<lastName>Doe</lastName>
</Person>

Do check out the documentation https://pypi.org/project/dicttoxml/ as you can pass additional arguments to alter the output.

XML to/from a Python dictionary

Question Serialize Python dictionary to XML lists some ways of XML serialization. As for alternative serialization formats, I guess pickle module is a nice tool for it.



Related Topics



Leave a reply



Submit