Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6664091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:34:18+00:00 2026-05-26T02:34:18+00:00

I have a XML file that I read with Python. I want to make

  • 0

I have a XML file that I read with Python. I want to make some changes in the XML file and write it back out.

Here is my code:

from xml.dom.minidom import *

filename = "file.xml"

dom = xml.dom.minidom.parse(filename)

dicts = dom.getElementsByTagName("dict")

for dict in dictList:  
    keys = dict.getElementsByTagName("key")
    for key in keys:
        keyCData = key.firstChild.wholeText
        if keyCData == "kind":
            print keyCData #prints "kind"
            key.firstChild.wholeText = "new text" 
            print key.firstChild.wholeText #prints "new text"

f = open("temp.xml", 'w')
dom.writexml(f)
f.close()

When I open “temp.xml” to look at though, all my elements with the “key” tag still have their CData as “kind” instead of “new text”. So how do I get the new data to be written out to the file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:34:19+00:00Added an answer on May 26, 2026 at 2:34 am

    Replace

    key.firstChild.wholeText = "new text"
    

    with either

    key.firstChild.replaceWholeText("new text")
    

    or

    key.firstChild.data = "new text"
    

    The key here is that xml.dom.minidom.Text.wholeText is a data descriptor, meant to be used more like a function than like an attribute. In fact, it collects data from nearby text and cdata nodes, in addition to its own data. Unfortunately, its setter doesn’t seem to be getting called, so by writing to wholeText, you are overriding the function. The writexml() implementation, however, only looks at the data attribute, not wholeText.

    This could be seen as a bug. In fact, someone could probably link replaceWholeText() as the setter for the wholeText property, but it may have to bypass the magic that the module uses to work with older versions of Python.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have xml-file. I need to read it, make some changes and write new
I have an xml-file that contains data that I want to read. The code
So I have some code that can read the methods out of a .coverage
I have a really simple XML file that I'm trying to read, but I
I have an XML file that is very long, but here is a shot
I have an XML file that I'm trying to serialize into an object. Some
I have an xml file that I want to configure using a bash script.
I have an XML file that I would like to map some attributes of
I currently have a php file that must read hundreds of XML files, I
I have a xml file that changed when i update some values, and i'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.