xml.dom.minidom appendChild/toxml formatting problem
Uche Ogbuji
uche at ogbuji.net
Sat Nov 6 21:34:51 EST 2004
rrarey at gmail.com (Ray) wrote in message news:<6471d296.0411051330.1477041 at posting.google.com>...
[SNIP]
> after calling domi.normalize(), domi.toxml(), and
> configFile.write(), the XML looks like:
> <Cluster Name="RAYTEST">
> <Cluster Name="RAYTEST2"><Cluster Name="RAYTEST3"/></Cluster>
> </Cluster>
>
> instead of how I would like it to look, which would be:
> <Cluster Name="RAYTEST">
> <Cluster Name="RAYTEST2">
> <Cluster Name="RAYTEST3"/>
> </Cluster>
> </Cluster>
>
> Or, if I append "RAYTEST3" to "RAYTEST", it comes out as:
> <Cluster Name="RAYTEST">
> <Cluster Name="RAYTEST2"/>
> <Cluster Name="RAYTEST3"/></Cluster>
>
> Instead of:
> <Cluster Name="RAYTEST">
> <Cluster Name="RAYTEST2"/>
> <Cluster Name="RAYTEST3"/>
> </Cluster>
>
>
> The rest of the XML file retains its original whitespace and
> linebreaks. How can I get this to format the new changes correctly?
> Calling toprettyxml instead of toxml seems to double the whitespace
> and linebreaks in the rest of the file.
There are many solutions to this, all of which require that you
properly understand what nodes are really there in a given DOM (and in
particular, understanding exactly how text nodes work).
One good way I've found for getting people familiar with the actual
DOM tree structure is using a GUi tree widget. See, for instance
listing 2 in:
http://www.xml.com/pub/a/2003/04/09/py-xml.html
Anyway, for the "pretty printing confused thinsg by only adding
whitespace" complaint, I suggest you strip whitespace first. There
are many libraries with routines for this, but you could start with
the following cookbook recipe if you like:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303061
Good luck.
--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://4Suite.org http://fourthought.com
A hands-on introduction to ISO Schematron -
http://www-106.ibm.com/developerworks/edu/x-dw-xschematron-i.html
Schematron abstract patterns -
http://www.ibm.com/developerworks/xml/library/x-stron.html
Wrestling HTML (using Python) -
http://www.xml.com/pub/a/2004/09/08/pyxml.html
XML's growing pains - http://www.adtmag.com/article.asp?id=10196
XMLOpen and more XML Hacks -
http://www.ibm.com/developerworks/xml/library/x-think27.html
A survey of XML standards -
http://www-106.ibm.com/developerworks/xml/library/x-stand4/
More information about the Python-list
mailing list