views:

55

answers:

5

Using elementree, the easiest way to read the text of a tag is to do the following:

import elementtree.ElementTree as ET
sKeyMap = ET.parse("KeyMaps/KeyMap_Checklist.xml")
host = sKeyMap.findtext("/BrowserInformation/BrowserSetup/host")

Now I want to update the text in the same file, hopefully without having to re-write it with something easy like:

host = "4444"
sKeyMap.replacetext("/BrowserInformation/BrowserSetup/host")

Any ideas?

Thx in advance Christopher

+1  A: 

If you want to update the value of the <host> element in your text file you should get a handle to the element using find() rather than just reading the text using findtext(). Once you have the element you can easily get the text out using element.text. Since you have the element you can easily reset its value as shown below:

import elementtree.ElementTree as ET
sKeyMap = ET.parse("KeyMaps/KeyMap_Checklist.xml")
host_element = sKeyMap.find("/BrowserInformation/BrowserSetup/host")
host = host_element.text
print host
# Now reset the the text of the <host> element
host = "4444"
host_element.text = host
Tendayi Mawushe
A: 

Thx for the answer but how do I save the changed text back to the file?

Christopher
+1  A: 

building on Tendayi's example maybe try something like:

newXmlContent = ET.tostring(sKeyMap)
fileObject = open("KeyMaps/newKeyMap_Checklist.xml","w") #note I used a different filename for testing!
fileObject.write(newXmlContent)
fileObject.close()
Luke Stanley
A: 

Maybe I'm not doing something write but I'm getting the error message "Exceptions: AssertionError" when the line "newXmlContent = ET.tostring(sKeyMap)" is executed. Below is the "complete" code to make it easier to see what I'm doing and to resolve the error.

import elementtree.ElementTree as ET sKeyMap = ET.parse("KeyMap_Checklist.xml") port_element = sKeyMap.find("/BrowserInformation/BrowserSetup/port") port = port_element.text print port port = "2222" port_element.text = port newXmlContent = ET.tostring(sKeyMap) fileObject = open("KeyMap_Checklist.xml", "w") fileObject.write(newXmlContent) fileObject.close() port_element = sKeyMap.find('/BrowserInformation/BrowserSetup/port') port = port_element.text print port

Thx, Christopher

Christopher
Why don't you edit this post and indent that code with 4 spaces so we can read it? Also, how about adding the whole exception backtrace so we can see what happened?
Forest
A: 

Sorry for the poor editing. In the code below, it should do the following but fails, step #4, when trying to write a value back to the XML file.

  1. Open the XML file
  2. Read a value
  3. Change the value
  4. Write the changed value back to the file, to the exact tab it was gotten from.

    import elementtree.ElementTree as ET sKeyMap = ET.parse("KeyMap_Checklist.xml") port_element = sKeyMap.find("/BrowserInformation/BrowserSetup/port") port = port_element.text print port port = "2222" port_element.text = port newXmlContent = ET.tostring(sKeyMap) fileObject = open("KeyMap_Checklist.xml", "w") fileObject.write(newXmlContent) fileObject.close() port_element = sKeyMap.find('/BrowserInformation/BrowserSetup/port') port = port_element.text print port

Christopher