Reply
Thread Tools
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.
 
Munk's Avatar
Posts: 229 | Thanked: 108 times | Joined on Oct 2007 @ Sacramento, California
#2
I believe the expression is *bump*
 

The Following User Says Thank You to Munk For This Useful Post:
epage's Avatar
Posts: 1,684 | Thanked: 1,562 times | Joined on Jun 2008 @ Austin, TX
#3
My understanding of your question is that you want to write only a field rather than the entire file?

My guess is that you cannot do this with the standard etree api. To me it seems this would require either etree to keep track of the original file's structure (lines/columns of each node's data) and fear the file structure changing behind its back or to reparse the file to perform the limited write which still could have a read/update race condition.
 

The Following User Says Thank You to epage For This Useful Post:
Munk's Avatar
Posts: 229 | Thanked: 108 times | Joined on Oct 2007 @ Sacramento, California
#4
epage, thank you for the response. I see what you are saying but I also believe that a simple "search and replace" kind of mentality would be built into a simple command. Something along the lines of:

ElementTree.updateNode('blah', 'new blah')

Otherwise, how would one recommend going about a file based find and replace command? So, open file X, replace the occurrence of "<somefield>blah</somefield>", overwrite the file. In other words, treat the .xml file as just a text file, update it in that mentality.
 
Posts: 1 | Thanked: 0 times | Joined on Jul 2010
#5
Hi Munk,

I am sticking here in the same problem if u find any updates let me know.
 
clasificado's Avatar
Posts: 466 | Thanked: 180 times | Joined on Feb 2010
#6
im not an experienced python developer, but in other platforms there are two ways to read and write xml data without using a simple stream: dom and sax.

with dom, you talk to the entire document, load it in memory, modify what you want and save it again where you want, the whole document. This is the "default" expected behaviour because of the nature of xml, that is a document based data storage and is not optimized for cherry pick access, but for a coherent multidimensional human readable structure.

the alternative is to use sax, that lets you read (and write) the xml from a parser point of view, in a way where the nodes comes in a stream like fashion so you dont need to read the entire document if your element is at the start of the xml.

in my experience, a situation that a document based read/save doesnt fits your needs could fall into two categories:
- you need to manage a really big xml of data (so a whole read/save is a several seconds stress operation), here is ok to use sax
- you need to read and write your xml a lot, for procedural storage. like saving your current gps position or a log or a status report. then you must consider the use of SQLite in the place of a simple XML, because the default behaviour of a rmdb is to save only the bytes needed as you wants

hope this helps

Last edited by clasificado; 2010-07-27 at 11:19.
 
Reply

Tags
python elementtree


 
Forum Jump


All times are GMT. The time now is 23:01.