Fromatting an xml file

uche.ogbuji at gmail.com uche.ogbuji at gmail.com
Tue Feb 7 13:21:36 EST 2006


sir_alex wrote:
> Hi! I have a little problem writing xml files formatted in a way like
> the following:
>
> <rootnode>
>          <node>bla</node>
>          <node>bla</node>
> </rootnode>
>
> Every new node element should have a tabulation before it, but when I
> use xml.dom.minidom I use writexml, which considers as a new node also
> the text (in my little example, "bla" phrases), so the best result I
> achieved has been the following
>
> <rootnode>
>         <node>
>                  bla
>         </node>
> </rootnode>
>
> but I don't want the text to be written on newlines... is there a good
> solution? Thanks!

That minidom behavior is fairly unsafe.  4Suite's PrettyPrinter is much
safer:

>>> from Ft.Xml import Parse
>>> from Ft.Xml.Domlette import PrettyPrint
>>> XML = "<rootnode><node>bla</node><node>bla</node></rootnode>"
>>> doc = Parse(XML)
>>> PrettyPrint(doc)
<?xml version="1.0" encoding="UTF-8"?>
<rootnode>
  <node>bla</node>
  <node>bla</node>
</rootnode>
>>>

http://4Suite.org

--
Uche Ogbuji                               Fourthought, Inc.
http://uche.ogbuji.net                    http://fourthought.com
http://copia.ogbuji.net                   http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/




More information about the Python-list mailing list