472,791 Members | 1,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

xml.dom.minidom weirdness: bug?

JYA
Hi.

I was writing an xmltv parser using python when I faced some weirdness
that I couldn't explain.

What I'm doing, is read an xml file, create another dom object and copy
the element from one to the other.

At no time do I ever modify the original dom object, yet it gets modified.

Unless I missed something, it sounds like a bug to me.

the xml file is simply:
<?xml version="1.0" encoding="utf-8"?>
<tv><channel id="id1"><display-name lang="en">full
name</display-name></channel></tv>

which I store under the name test.xmltv

Here is the code, I've removed everything that isn't applicable to my
description. can't make it any simpler I'm afraid:

from xml.dom.minidom import Document
import xml.dom.minidom
def adjusttimezone(docxml, timezone):
doc = Document()

# Create the <tvbase element
tv_xml = doc.createElement("tv")
doc.appendChild(tv_xml)

#Create the channel list
channellist = docxml.getElementsByTagName('channel')

for x in channellist:
#Copy the original attributes
elem = doc.createElement("channel")
for y in x.attributes.keys():
name = x.attributes[y].name
value = x.attributes[y].value
elem.setAttribute(name,value)
for y in x.getElementsByTagName('display-name'):
elem.appendChild(y)
tv_xml.appendChild(elem)

return doc

if __name__ == '__main__':
handle = open('test.xmltv','r')
docxml = xml.dom.minidom.parse(handle)
print 'step1'
print docxml.toprettyxml(indent=" ",encoding="utf-8")
doc = adjusttimezone(docxml, 1000)
print 'step2'
print docxml.toprettyxml(indent=" ",encoding="utf-8")

Now at "step 1" I will display the content of the dom object, quite
natually it shows:
<?xml version="1.0" encoding="utf-8"?>
<tv>
<channel id="id1">
<display-name lang="en">
full name
</display-name>
</channel>
</tv>

After a call to adjusttimezone, "step 2" however will show:
<?xml version="1.0" encoding="utf-8"?>
<tv>
<channel id="id1"/>
</tv>

That's it !

You'll note that at no time do I modify the content of docxml, yet it
gets modified.

The weirdness disappear if I change the line
channellist = docxml.getElementsByTagName('channel')
to
channellist = copy.deepcopy(docxml.getElementsByTagName('channel '))

However, my understanding is that it shouldn't be necessary.

Any thoughts on this weirdness ?

Thanks
Jean-Yves

--
They who would give up an essential liberty for temporary security,
deserve neither liberty or security (Benjamin Franklin)

Jun 27 '08 #1
2 2322
En Tue, 29 Apr 2008 23:51:14 -0300, JYA <no****@nospam.blahescribió:
What I'm doing, is read an xml file, create another dom object and copy
the element from one to the other.

At no time do I ever modify the original dom object, yet it gets
modified.

for y in x.getElementsByTagName('display-name'):
elem.appendChild(y)
tv_xml.appendChild(elem)
You'll note that at no time do I modify the content of docxml, yet it
gets modified.

The weirdness disappear if I change the line
channellist = docxml.getElementsByTagName('channel')
to
channellist = copy.deepcopy(docxml.getElementsByTagName('channel '))

However, my understanding is that it shouldn't be necessary.
I think that any element can have only a single parent. If you get an
element from one document and insert it onto another document, it gets
removed from the first.

--
Gabriel Genellina

Jun 27 '08 #2
JYA <no****@nospam.blahwrote:
for y in x.getElementsByTagName('display-name'):
elem.appendChild(y)
Like Gabriel wrote, nodes can only have one parent. Use
elem.appendChild(y.cloneNode(True))
instead. Or y.cloneNode(False), if you want a shallow copy (i.e. without
any of the children, e.g. text content).

Marc
Jun 27 '08 #3

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...
3
by: Sunil Movva | last post by:
I have an application that uses xml to communicate between threads. One of the threads in my app creates an xml message and sends it to a second thread. This second thread parses the message and...
5
by: Skip Montanaro | last post by:
I'd like to compare two xml.dom.minidom objects, but the naive attempt fails: >>> import xml.dom.minidom >>> d1 = xml.dom.minidom.parse("ES.xml") >>> d2 = xml.dom.minidom.parse("ES.xml") >>> d1...
5
by: Mike McGavin | last post by:
Hi everyone. I've been trying for several hours now to get minidom to parse namespaces properly from my stream of XML, so that I can use DOM methods such as getElementsByTagNameNS(). For some...
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...
18
by: sim.sim | last post by:
Hi all. i'm faced to trouble using minidom: #i have a string (xml) within CDATA section, and the section includes "\r\n": iInStr = '<?xml version="1.0"?>\n<Data><!]></Data>\n' #After i...
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...
3
by: aine_canby | last post by:
Hi, I'm working with a number of scripts which were written years ago for my company for Python 2.2, and I'd like to update for Python 2.5. I have written a script to add # -*- coding: cp1252...
2
by: ashmir.d | last post by:
Hi, I am trying to parse an xml file using the minidom parser. <code> from xml.dom import minidom xmlfilename = "sample.xml" xmldoc = minidom.parse(xmlfilename) </code> The parser is...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.