Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > xml.dom.minidom appendChild/toxml formatting problem

Reply
Thread Tools

xml.dom.minidom appendChild/toxml formatting problem

 
 
Ray
Guest
Posts: n/a
 
      11-05-2004
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.
 
Reply With Quote
 
 
 
 
Jeremy Jones
Guest
Posts: n/a
 
      11-05-2004
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

 
Reply With Quote
 
 
 
 
Uche Ogbuji
Guest
Posts: n/a
 
      11-07-2004
(Ray) wrote in message news:< 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/
 
Reply With Quote
 
Ray
Guest
Posts: n/a
 
      11-08-2004
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:<. com>...
> (Ray) wrote in message news:< 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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Asp.Net/Access 2002 text formatting problem ald ASP .Net 0 12-04-2003 08:13 PM
Need formatting options menu for formatting hard drive Mark T. Computer Support 3 11-24-2003 11:50 PM
Date Formatting problem Kerri ASP .Net 3 08-21-2003 02:29 PM
Date Formatting problem Kerri ASP .Net 0 08-20-2003 07:24 PM
Date Formatting problem Kerri ASP .Net 0 08-20-2003 07:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57