[XML-SIG] Help writing out xml

Harry George hgg9140 at seanet.com
Wed Jan 12 03:33:39 CET 2005

An option is to write the xml manually.   That is, each object in your
"model" (as in model-view-controller) has a "to_xml" method which takes
a fileobject (sys.stdout is default), and writes itself in XML to
that file.   

Indents are controlled by some formatting mechanism.  I
personally use my "tabbedwriter":

I've done this with a lot of XML's and find it is easy to control and
trivial to setup.   Where there is inheritance in the "model", a lot of
the XML output can be done by the base object(s).

On Tue, 11 Jan 2005 17:07:14 -0500
Willis <willismonroe at verizon.net> wrote:

> 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>wil
> lis</account>").toprettyxml("","\n","UTF-8")).getElementsByTagName("a
> ccount")[0].childNodes[0].data = u'\n willis\n' 
> xml.dom.minidom.parseString(xml.dom.minidom.parseString("<account>wil
> lis</account>").toxml("UTF-8")).getElementsByTagName("account")[0].ch
> ildNodes[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
> _______________________________________________
> XML-SIG maillist  -  XML-SIG at python.org
> http://mail.python.org/mailman/listinfo/xml-sig

Harry George
hgg9140 at seanet.com  www.seanet.com/~hgg9140

More information about the XML-SIG mailing list