Python Minidom : Change Value of Node

Peter-W picture Peter-W · Nov 10, 2011 · Viewed 9.6k times · Source

I'm using Python's minidom library to try and manipulate some XML files. Here is an example file :

<document>
    <item>
            <link>http://www.this-is-a-url.com/</link>
            <description>This is some information!</description>
    </item>

    <item>
            <link>http://www.this-is-a-url.com/</link>
            <description>This is some information!</description>
    </item>

    <item>
            <link>http://www.this-is-a-url.com/</link>
            <description>This is some information!</description>
    </item>
</document>

What I need to do, is take the value in "description" and put it into "link" so both say "This is some information!". I've tried to do it like so:

#!/usr/bin/python

from xml.dom.minidom import parse

xmlData = parse("file.xml")

itmNode = xmlData.getElementsByTagName("item")
for n in itmNode:
    n.childNodes[1] = n.childNodes[3]
    n.childNodes[1].tagName = "link"
print xmlData.toxml()

However "n.childNodes[1] = n.childNodes[3]" seems to link the two nodes together, so when I do "n.childNodes[1].tagName = "link"" to correct the name BOTH child nodes become "link" where before they were both "description".

Furthermore, if I use "n.childNodes[1].nodeValue" the changes don't work and the XML is printed in it's original form. What am I doing wrong?

Answer

jcollado picture jcollado · Nov 11, 2011

I'm not sure you can modify the DOM in place with xml.dom.minidom (creating the whole document from scratch with new values should work though).

Anyway, if you accept a solution based on xml.etree.ElementTree (I strongly recommend using it since it provides a friendlier interface), then you could use the following code:

from xml.etree.ElementTree import ElementTree, dump

tree = ElementTree()
tree.parse('file.xml')

items = tree.findall('item')
for item in items:
    link, description = list(item)
    link.text = description.text

dump(tree)