0

I'm trying to search a tag and replace an element in some XML code. Here is my attempt:

from xml.dom import minidom

dom = minidom.parse('../../../lib/config/folder/config.xml')

for tag_type in dom.getElementsByTagName('tag_type'):
    tag_type.childNodes = [dom.createTextNode("Replacement Word")]

print tag_type

I'm running Python 2.4 so Element tree is not an option. Currently, I'm getting a response of:

<DOM Element: tag_type at 0x1c28a0e0>

Not sure why it isn't printing and not replacing.

6
  • ElementTree can be downloaded for Python 2.4. Grab your copy here, Python 1.5.2 (!) and newer are supported. Commented Aug 12, 2013 at 14:44
  • I can't download it. I'm stuck with what I got unfortunately. Thanks. Commented Aug 12, 2013 at 14:45
  • 1
    You cannot just assign to childNodes. You must use the DOM methods to manipulate the tree. Commented Aug 12, 2013 at 14:46
  • That's exactly what print tag_type would print. What did you expect? Commented Aug 12, 2013 at 14:46
  • It helps us greatly if you post what the input and desired output looks like. Commented Aug 12, 2013 at 14:48

1 Answer 1

3

You'll have to use the Node API to remove and add nodes:

for tag_type in dom.getElementsByTagName('tag_type'):
    while tag_type.hasChildNodes():
        tag_type.removeChild(tag_type.firstChild)
    tag_type.appendChild(dom.createTextNode("Replacement Word"))

Demo:

>>> from xml.dom import minidom
>>> xml = '''\
... <root>
...    <tag_type>
...       Foo
...       Bar
...    </tag_type>
... </root>
... '''
>>> dom = minidom.parseString(xml)
>>> for tag_type in dom.getElementsByTagName('tag_type'):
...     while tag_type.hasChildNodes():
...         tag_type.removeChild(tag_type.firstChild)
...     tag_type.appendChild(dom.createTextNode("Replacement Word"))
... 
<DOM Text node "'Replacemen'...">
>>> print dom.toxml()
<?xml version="1.0" ?><root>
   <tag_type>Replacement Word</tag_type>
</root>
Sign up to request clarification or add additional context in comments.

7 Comments

How do I grab a file from another folder?
I am not sure I follow you. Use a different filename when parsing the file?
When I try this method I get a bunch of errors. I think it's because my xml file is not in the same active folder as I'm running the script from. Do I need to set a PATH to look in another folder for the xml file?
@AwayFromMyDesk: What kind of errors are you getting? And how are you parsing the file? Note that I used parseString() in my sample code because I had the XML in a string value. Use .parse() if you have a filename instead.
I was getting a bunch of errors internal to parseString() at lines 1925, 942, 223. Obviously not in my script. I put it back to just parse() now I'm just getting an error: NameError: name 'appendChild' is not defined
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.