[XML-SIG] Help writing out xml

Willis willismonroe at verizon.net
Tue Jan 11 23:07:14 CET 2005


I'm writing a script that edits and writes xml to a
gaim(http://gaim.sourceforge.net) configuration file.  However the
problem I'm having is I've found no way of outputting xml in python that
preserves text nodes.

i.e. gaim writes 
...
<account>willis</account>
...

in it's xml file somehow, and the best I can get python to do is:
...
<account>
        willis
</account>
...
which gaim won't parse.

but I was curious how can the toprettyxml() function change the xml so
drastically that it parses itself differently as illustrated in these to
lines, where the only difference is the writing function (toprettyxml,
and toxml.


xml.dom.minidom.parseString(xml.dom.minidom.parseString("<account>willis</account>").toprettyxml("","\n","UTF-8")).getElementsByTagName("account")[0].childNodes[0].data = u'\n willis\n' 

xml.dom.minidom.parseString(xml.dom.minidom.parseString("<account>willis</account>").toxml("UTF-8")).getElementsByTagName("account")[0].childNodes[0].data = u'willis' 

I know that printing with toxml() works, however toxml() also writes
everything on one line, something that is very impractical for
supposedly readable files.

basically I'm looking for a way to print xml, where the output is nice
looking, but the text nodes are preserved perfectly, as in no extra line
breaks and tabs that toprettyxml() inserts on it's own.

thanks
-Willis




More information about the XML-SIG mailing list