473,569 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is it bug or feature in xml.dom.minidom ?


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.parseSt ring("<resp><do c><one>One</one><two>Two</two></doc></resp>")
_resp = minidom.parseSt ring("<resp><do c/></resp>")
iSourseTag = _requ.getElemen tsByTagName('do c')[0]
iTargetTag = _resp.getElemen tsByTagName('do c')[0]
# it prints me that there are two child nodes
for iChild in iSourseTag.chil dNodes:
print iChild.toxml()
# when i walk elements, only first iteration was made
# and iSourseTag.chil dNodes now have only one element instead of two
for iChild in iSourseTag.chil dNodes:
iTargetTag.appe ndChild(iChild)
# it prints me that there is only one child node
for iChild in iSourseTag.chil dNodes:
print iChild.toxml()

i'm not sure, whether i append child nodes in properly way, but IMHO it looks like a bug.

My question is how to avoid the "iSourseTag " changes while iterate its nodes?

And, of course, how to append all child nodes to "iTargetTag "?

Thank for any help.

--
Maksim Kasimov
Mar 2 '07 #1
1 1964
Maksim Kasimov wrote:
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.parseSt ring("<resp><do c><one>One</one><two>Two</two></doc></resp>")
_resp = minidom.parseSt ring("<resp><do c/></resp>")
Note that these are different documents - this is important later on.
iSourseTag = _requ.getElemen tsByTagName('do c')[0]
iTargetTag = _resp.getElemen tsByTagName('do c')[0]
# it prints me that there are two child nodes
for iChild in iSourseTag.chil dNodes:
print iChild.toxml()
Seems alright.
# when i walk elements, only first iteration was made
# and iSourseTag.chil dNodes now have only one element instead of two
for iChild in iSourseTag.chil dNodes:
iTargetTag.appe ndChild(iChild)
But since you're taking a node from one document to add it to another,
you should instead use importNode to make that node importable into
the target document:

for iChild in iSourseTag.chil dNodes:
# 1 or True should cause a deep copy
iNewChild = _resp.importNod e(iChild, 1)
iTargetTag.appe ndChild(iNewChi ld)
# it prints me that there is only one child node
for iChild in iSourseTag.chil dNodes:
print iChild.toxml()
That's probably because you've "stolen" the node from its document in
order to add it to the target document - something which is possibly
an artifact of the minidom implementation.
i'm not sure, whether i append child nodes in properly way, but IMHO it looks like a bug.
That minidom does not refuse to let you "move" nodes in this way could
be debated as being a bug or not, but the correct way of copying nodes
is to use importNode.
My question is how to avoid the "iSourseTag " changes while iterate its nodes?

And, of course, how to append all child nodes to "iTargetTag "?
These questions are hopefully answered above.

Paul

Mar 2 '07 #2

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

Similar topics

0
7486
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>
3
2158
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 creates a new xml message copying most part of the first message and sends it to a third thread. The second thread was able to parse the message,...
5
4806
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 == d2 False My goal is to decide whether or not I need to prompt the user to save config information at the end of a program run by generating a...
5
6359
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 reason, though, it just doesn't seem to want to split the prefixes from the rest of the tags when parsing. The minidom documentation at...
4
6050
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...
18
758
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 create DOM-object, i get the value of "Data" without "\r\n"
0
1509
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
3
1993
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 -*- to the beginning of all my scripts, and that has fixed the encoding issues. Another issue was the use of - from _xmlplus.dom import minidom
2
12557
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 failing on this line:
0
7924
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. ...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7974
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...
0
6284
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...
1
5513
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...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
938
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...

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.