3

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?

Peter-W
  • 737
  • 2
  • 9
  • 14

1 Answers1

5

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)
jcollado
  • 39,419
  • 8
  • 102
  • 133
  • Actually, you CAN. A helpful user showed how to modify with minidom. http://stackoverflow.com/questions/13588072/python-minidom-xml-how-to-set-node-text-with-minidom-api?lq=1 – Warren P Nov 28 '12 at 17:15
  • @WarrenP That's interesting. Thanks for sharing. – jcollado Nov 29 '12 at 00:08