Constructing and then Printing/Writing a simple XML file with dom

Published: Wednesday, Dec 26, 2007 Last modified: Monday, May 27, 2024

Here is a simple XML snippet:

<?xml version="1.0" ?>

<div class="test">
            Hello World


Now, here is a very simple example of how I would go about constructing it using python’s minidom in 2.2.

#!/usr/bin/env python2

import xml.dom.minidom

file_object = open("/tmp/test.xml", "w")

newdoc = xml.dom.minidom.Document()

div = newdoc.createElement("div")

div.setAttribute('class', 'test')


h1 = newdoc.createElement("h1")

headertext = newdoc.createTextNode('Hello World')



# Show it on screen

print newdoc.toprettyxml()



The written XML is not pretty mind:

hendry@bogrund-14:/tmp$ cat test.xml

<?xml version="1.0" ?>

<div class="test"><h1>Hello World</h1></div>hendry@bogrund-14:/tmp$

Advice: When creating text nodes it wants strings, not integers etc. You will get strange error messages when you do not pass it strings, which might cost you time debugging.