tags:

views:

1226

answers:

4
+2  A: 

Setting an attribute on an object won't give a compile-time or a run-time error, it will just do nothing useful if the object doesn't access it (i.e. "node.noSuchAttr = 'bar'" would also not give an error).

Unless you need a specific feature of minidom, I would look at ElementTree:

import sys
from xml.etree.cElementTree import Element, ElementTree

def make_xml():
    node = Element('foo')
    node.text = 'bar'
    doc = ElementTree(node)
    return doc

if __name__ == '__main__':
    make_xml().write(sys.stdout)
dF
+3  A: 

@Daniel

Thanks for the reply, I also figured out how to do it with the minidom (I'm not sure of the difference between the ElementTree vs the minidom)


from xml.dom.minidom import *
def make_xml():
    doc = Document();
    node = doc.createElement('foo')
    node.appendChild(doc.createTextNode('bar'))
    doc.appendChild(node)
    return doc
if __name__ == '__main__':
    make_xml().writexml(sys.stdout)

I swear I tried this before posting my question...

mmattax
+3  A: 

I found a pretty verbose tutorial on the minidom method

Here's a tutorial for the etree method. It's much nicer to read, and seems quite simple. It also goes over parsing of xml (briefly)

Jiaaro
The etree link is broken.
Emil
A: 

@Jim Robert

Thanks, I actually saw that but thought it was too verbose...

mmattax