473,698 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.createElem ent(elementType )
newCluster.setA ttribute("Name" , elementName)
elem.appendChil d(newCluster)
else:
elem.removeChil d(childElem)

domi.normalize( )

configFile = open(self.flist[0], "w")
newDoc = domi.toxml()
configFile.writ e(newDoc)
configFile.clos e()

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.writ e(), 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 3579
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.createElem ent(elementType )
newCluster.setA ttribute("Name" , elementName)
elem.appendChil d(newCluster)
else:
elem.removeChil d(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.co m (Ray) wrote in message news:<64******* *************** ***@posting.goo gle.com>...

[SNIP]
after calling domi.normalize( ), domi.toxml(), and
configFile.writ e(), 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.Pre ttyPrint().

news:<d1******* *************** ****@posting.go ogle.com>...
rr****@gmail.co m (Ray) wrote in message news:<64******* *************** ***@posting.goo gle.com>...

[SNIP]
after calling domi.normalize( ), domi.toxml(), and
configFile.writ e(), 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
7507
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 looks something like this: <item xmlns="" xmlns:thing="http://www.blah.com"> <thing:child name="smith"/> </item>
5
2163
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 Hetland "In this house we obey the laws of http://hetland.org thermodynamics!" Homer Simpson
2
10278
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 DOCUMENT SYSTEM "simple.dtd"> from the original xml sample. <?XML version="1.0" encoding="UTF-8"?>
1
2755
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() or parseString(), the namespace exists as an xmlns attribute in the DOM (fair enough, as it's in the original source document). However, if I use the DOM implementation to create an identical document with a namespace, the xmlns attribute is not...
6
3981
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: -------------------------------------------------- <?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html>
4
6065
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 script that grabs some web pages from the web, regex parse the data and stores it localy to xml file for further use.. at first i had no problem using python minidom and everything concerning
5
6559
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. Any suggestions? What it ends up doing is just removing the character from the
1
1970
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 = minidom.parseString("<resp><doc><one>One</one><two>Two</two></doc></resp>") _resp = minidom.parseString("<resp><doc/></resp>") iSourseTag = _requ.getElementsByTagName('doc') iTargetTag = _resp.getElementsByTagName('doc')
0
1513
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 problem was handled in CVS a while ago. http://mail.python.org/pipermail/xml-sig/2003-October/009904.html
0
8671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8856
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4613
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.