xml.dom.minidom appendChild/toxml formatting problem

R

Ray

I have put together some pretty simple code for adding and removing
elements from an XML file, but am having a problem with toxml writing
out the correct format after I have called appendChild on a node.

Here is a snippet:

if (action == 'add'):
newCluster = domi.createElement(elementType)
newCluster.setAttribute("Name", elementName)
elem.appendChild(newCluster)
else:
elem.removeChild(childElem)

domi.normalize()

configFile = open(self.flist[0], "w")
newDoc = domi.toxml()
configFile.write(newDoc)
configFile.close()

Basically, if I start out with a Cluster that looks like this in the
XML file:
<Cluster Name="RAYTEST">
<Cluster Name="RAYTEST2"/>
</Cluster>

and then use the createElement, setAttribute, and appendChild lines
from above to add a new Cluster called "RAYTEST3" to the "RAYTEST2"
Cluster, 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.
 
J

Jeremy Jones

Ray said:
I have put together some pretty simple code for adding and removing
elements from an XML file, but am having a problem with toxml writing
out the correct format after I have called appendChild on a node.

Here is a snippet:

if (action == 'add'):
newCluster = domi.createElement(elementType)
newCluster.setAttribute("Name", elementName)
elem.appendChild(newCluster)
else:
elem.removeChild(childElem)

domi.normalize()
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.
I'd like to offer you a more automated method, but I can't. Perhaps
someone else can. The only way I can think of to do it is to see what
kind of text nodes exist around where you are inserting your elements
and get that for formatting and insert similar text nodes before you
insert your elements.

Just out of curiosity, why are you interested in preserving the
whitespace? Is this going to be something that is human read rather
than machine read (or human read in addition to machine read)?


Jeremy Jones
 
U

Uche Ogbuji

(e-mail address removed) (Ray) wrote in message
[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/
 
R

Ray

Thanks for the suggestions, guys. I was able to get it to do what I
wanted by replacing my call with toprettyxml() with a call to
xml.dom.ext.PrettyPrint().

(e-mail address removed) (Ray) wrote in message
[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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top