how to print newline in xml?

uche.ogbuji at gmail.com uche.ogbuji at gmail.com
Sun Jun 4 11:49:35 EDT 2006


anatoli.barski at googlemail.com 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
doc.toprettyxml()

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
.removeChild

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

Good luck.

[1] http://www-128.ibm.com/developerworks/xml/library/x-c14n/

--
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