2

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

2 Answers 2

1

You have not checked the

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

docs.

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.