By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,682 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

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

P: n/a

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')[0]
iTargetTag = _resp.getElementsByTagName('doc')[0]
# it prints me that there are two child nodes
for iChild in iSourseTag.childNodes:
print iChild.toxml()
# when i walk elements, only first iteration was made
# and iSourseTag.childNodes now have only one element instead of two
for iChild in iSourseTag.childNodes:
iTargetTag.appendChild(iChild)
# it prints me that there is only one child node
for iChild in iSourseTag.childNodes:
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.parseString("<resp><doc><one>One</one><two>Two</two></doc></resp>")
_resp = minidom.parseString("<resp><doc/></resp>")
Note that these are different documents - this is important later on.
iSourseTag = _requ.getElementsByTagName('doc')[0]
iTargetTag = _resp.getElementsByTagName('doc')[0]
# it prints me that there are two child nodes
for iChild in iSourseTag.childNodes:
print iChild.toxml()
Seems alright.
# when i walk elements, only first iteration was made
# and iSourseTag.childNodes now have only one element instead of two
for iChild in iSourseTag.childNodes:
iTargetTag.appendChild(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.childNodes:
# 1 or True should cause a deep copy
iNewChild = _resp.importNode(iChild, 1)
iTargetTag.appendChild(iNewChild)
# it prints me that there is only one child node
for iChild in iSourseTag.childNodes:
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 discussion thread is closed

Replies have been disabled for this discussion.