3
<CCC>
    <BBB>This is test</BBB>
</CCC>

Here I need to modify the CCC to XXX. How do I do this using minidom and Python?

Expected Output:

<XXX>
    <BBB>This is test</BBB>
</XXX>

2 Answers 2

5

You can change the node name by setting the tagName attribute Try this,

tag_ccc = dom2.getElementsByTagName("CCC")[0]
tag_ccc.tagName = "XXX"

This should change the tag name to "XXX", below is the test code i used to confirm this using python 2.7

from xml.dom.minidom import parse, parseString
xml ="""<CCC><BBB>This is test</BBB></CCC>"""    
dom = parseString(xml)
tag_ccc = dom.getElementsByTagName("CCC")[0]
tag_ccc.tagName = "XXX"
print tag_ccc.toxml("utf-8")

Hope this helped.

Sign up to request clarification or add additional context in comments.

Comments

1

You can change the element name by modifying the tagName of the node. For instance:

root = dom.getElementsByTagName('CCC')[0]
root.tagName = 'XXX'

You get:

<XXX>
    <BBB>This is test</BBB>
</XXX>

The documentation is available here.

Comments

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.