[XML-SIG] xml.dom.minidom question

Hashemian, Mehdi mehdi.hashemian at spirentcom.com
Fri Jul 23 23:10:05 CEST 2004


> So is your problem with the actual composition of cloned text nodes, or
> with the way they're handled by prettyprint?  

I am not sure. Originally, I thought it is the way toprettyxml() works but I
see the same behavior with toxml(). Now, I start thinking that maybe in
every stage: reading and parsing, copying node, writing to a new file, a new
'\n' is added to the file, more like a composition problem.

> You may want to show some code in order to clarify 
> the problem for anyone who can help you.
___________________________________________________

from xml.dom import Node

import xml.dom.minidom
impl = xml.dom.minidom.getDOMImplementation()

newDoc = impl.createDocument(None, u'metaInfo', None)
topEle = newDoc.documentElement

fileName = "orig.xml"
file = open(fileName, 'r')
document = xml.dom.minidom.parse(file)

for node in document.getElementsByTagName("components"):
	if node.nodeType == Node.ELEMENT_NODE:
	      newCompsNode = topEle.appendChild(node.cloneNode(True))

newFileName = "mehdi.xml"
newFile = open(newFileName, 'w')
newFile.write(newDoc.toprettyxml())
___________________________________________________
fileName (orig.xml):

<?xml version="1.0" encoding="UTF-8"?>

<components>
	<component name="xxx.cspec" version="1.0"/>
</components>
____________________________________________________
newFileName (mehdi.xml):

<?xml version="1.0" encoding="UTF-8"?>

<components>



	<component name="xxx.cspec" version="1.0"/>



</components>
___________________________________________________

Thanks,
Mehdi


-----Original Message-----
From: Uche Ogbuji [mailto:uche.ogbuji at fourthought.com]
Sent: Friday, July 23, 2004 11:33 AM
To: Hashemian, Mehdi
Cc: 'xml-sig at python.org'
Subject: Re: [XML-SIG] xml.dom.minidom question

On Wed, 2004-07-21 at 19:00, Hashemian, Mehdi wrote:
> Hello,
>  
> I apologize if I am sending my question to the wrong email list.

It's the right list.

> I am trying to copy a node and its children from one XML document to
> another one. I clone the node from document A and then append it to
> the root node in document B. If I have elements of copied node in document
> A correctly indented with '\n', in the new document, for each new line I
> have three new lines. When I remove the new Lines from document A,
> every thing looks fine in document B.
>  
> I use toprettyxml function to print document to a file.
> I use xml.dom.minidom module in python 2.2.2 on Red Hat 9.0.

So is your problem with the actual composition of cloned text nodes, or
with the way they're handled by prettyprint?  You may want to show some
code in order to clarify the problem for anyone who can help you.

-- 
Uche Ogbuji                                    Fourthought, Inc.
http://uche.ogbuji.net    http://4Suite.org    http://fourthought.com
Perspective on XML: Steady steps spell success with Google -
http://www.adtmag.com/article.asp?id=9663
Use XML namespaces with care -
http://www-106.ibm.com/developerworks/xml/library/x-namcar.html
Managing XML libraries - http://www.adtmag.com/article.asp?id=9160
Commentary on "Objects. Encapsulation. XML?" -
http://www.adtmag.com/article.asp?id=9090
A survey of XML standards -
http://www-106.ibm.com/developerworks/xml/library/x-stand4/


More information about the XML-SIG mailing list