473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

xml.dom.minidom appendChild/toxml formatting problem

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.
Jul 18 '05 #1
3 3556
Ray wrote:
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()

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

Jul 18 '05 #2
rr****@gmail.com (Ray) wrote in message news:<64*************************@posting.google.c om>...

[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/Coo.../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/developerwork...ematron-i.html
Schematron abstract patterns -
http://www.ibm.com/developerworks/xm...y/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/xm...x-think27.html
A survey of XML standards -
http://www-106.ibm.com/developerwork...rary/x-stand4/
Jul 18 '05 #3
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().

news:<d1**************************@posting.google. com>...
rr****@gmail.com (Ray) wrote in message news:<64*************************@posting.google.c om>...

[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/Coo.../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/developerwork...ematron-i.html
Schematron abstract patterns -
http://www.ibm.com/developerworks/xm...y/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/xm...x-think27.html
A survey of XML standards -
http://www-106.ibm.com/developerwork...rary/x-stand4/

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: xtian | last post by:
Hi - I'm doing some data conversion with minidom (turning a csv file into a specific xml format), and I've hit a couple of small problems. 1: The output format has a header with some xml that...
5
by: Magnus Lie Hetland | last post by:
Is it possible to build a minidom tree bottom-up without access to a document node? It seems that simply instantiating the various node classes doesn't quite do the trick... -- Magnus Lie...
2
by: Anthony Liu | last post by:
I copy-pasted the following sample xml document from http://slis-two.lis.fsu.edu/~xml/sample.html and saved it as samplexml.xml. Please note that I removed the following line <!DOCTYPE...
1
by: Greg Wogan-Browne | last post by:
Hi all, I am having some trouble figuring out what is going on here - is this a bug, or correct behaviour? Basically, when I create an XML document with a namespace using xml.dom.minidom.parse()...
6
by: Horst Gutmann | last post by:
Hi :-) I currently have quite a big problem with minidom and special chars (for example &uuml;) in HTML. Let's say I have following input file:...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
5
by: fscked | last post by:
Hi guys/gals. I am trying to write and xml file from data parsed from a csv. I can get everything to work except that I cannot get minidom to do --> ö which needless to say is driving me nuts....
1
by: Maksim Kasimov | last post by:
Hi, i'm faced with such a problem when i use xml.dom.minidom: to append all child nodes from "doc" in "_requ" to "doc" in "_resp", i do the following: _requ =...
0
by: Gary | last post by:
Howdy I ran into a difference between Python on Windows XP and Linux Fedora 6. Writing a dom to xml with minidom works on Linux. It gives an error on XP if there is an empty namespace. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.