how to print newline in xml?

uche.ogbuji at uche.ogbuji at
Sun Jun 4 17:49:35 CEST 2006

anatoli.barski at wrote:
> I use Python/XML packages are xml.dom.minidom and xml.dom.ext (second
> just for PrettyPrint)

You don't need xml.dom.ext for prettyprint.  You can use

I gather you want to tweak the prettyprinter to not add the newline
before the comment.  The only way to do this is to write your own
printing logic, which is really not that hard, if you just start by
copying the code from .writexml (used by .toprettyxml).

But there's an even easier (if slower) way: pretty print the document,
then parse it in again, remove the text node between the element in
question and the following comment, and then use .writexml() to
serialize it it again.

A few general notes:

* You cannot set the order of attributes in most XML tools, whether
Python or not.  This is unfortunate for people who would like to
preserve such details for usability reasons, but that's just the way
XML is.  The closest you can get is by using canonicalization [1],
which is available in PyXML as xml.dom.ext.c14n.  It just so happens
that canonical XML leaves the attributes in the order you want.  You
won't always be so lucky.

* You can always create text nodes by using doc.createTextNode.

* You can always remove text nodes (or any other kind) by using

* It's much easier to navigate if you use XPath.  PyXML has an
xml.xpath module you can use.

Good luck.


Uche Ogbuji                               Fourthought, Inc.                   

More information about the Python-list mailing list