View Single Post
Munk's Avatar
Posts: 229 | Thanked: 108 times | Joined on Oct 2007 @ Sacramento, California
#1
Hi Python gurus. I'm trying to write XML data out to my filed called "config.xml" but am having a difficult time. I'm not having a problem reading information that I manually entered into the file. In my attempts I am able to append new XML nodes to this file but when I try to replace/update one of the elements I don't get what I intended.

Is there a simple way to just replace one element? Or do I have to read all elements from a file and then write the whole dang thing out just to update one element?

Here's a sample of my config.xml file:
Code:
<traveller>
  <time_format>%I:%M</time_format>
  <location>95838</location>
  <weather_refresh_interval>1</weather_refresh_interval>
  <celsius>False</celsius>

  <atmosphere>
    <title>rain3</title>
    <volume>5</volume>
    <playing>False</playing>
  </atmosphere>
  <atmosphere>
    <title>Children Playing</title>
    <volume>3</volume>
    <playing>False</playing>
  </atmosphere>
  <atmosphere>
    <title>Grandfather Clock</title>
    <volume>7</volume>
    <playing>True</playing>
  </atmosphere>

</traveller>
As the test, I'm trying to update "location" from 95838 to something else. If you have any code snippets I would appreciate it. I tried searching for hours but didn't find the silver bullet. I have researched this for a couple of days now and am just getting frustrated. I'm using ElementTree including it in this manner:

Code:
from xml.etree import ElementTree as ET
Thanks in advance.