yudistrange
yudistrange

Reputation: 515

XML creation from a Dictionary in Python

I am quite new to XML as well as Python, so please overlook . I am trying to unpack a dictionary straight into XML format. My Code Fragment is as follows:

from xml.dom.minidom import Document
def make_xml(dictionary):
    doc = Document()
    result = doc.createElement('result')
    doc.appendChild(result)    

    for key in dictionary:
        attrib = doc.createElement(key)
        result.appendChild(attrib)
        value = doc.createTextNode(dictionary[key])
        attrib.appendChild(value)

    print doc

I expected an answer of the format

<xml>
 <result>
  <attrib#1>value#1</attrib#1>
   ...

However all I am getting is

<xml.dom.minidom.Document instance at 0x01BE6130>

Please help

Upvotes: 2

Views: 255

Answers (2)

watzon
watzon

Reputation: 2549

You can always use a library like xmler which easily takes a python dictionary and converts it to xml. Full disclosure, I created the package, but I feel like it will probably do what you need.

Also feel free to take a look at the source.

Upvotes: 0

user2665694
user2665694

Reputation:

You have not checked the

http://docs.python.org/library/xml.dom.minidom.html

docs.

Look at the toxml() or prettyprettyxml() methods.

Upvotes: 1

Related Questions