I have an xml file, which contains default settings for a GUI app, controlling a device:
<Settings>
<Setting Name="Frame0_TypeTextBox" Type="UpDown" UpDownType="d">1</Setting>
<Setting Name="Frame1_TypeTextBox" Type="UpDown" UpDownType="d">2</Setting>
<Setting Name="Frame2_TypeTextBox" Type="UpDown" UpDownType="d">1</Setting>
<Setting Name="messageTypeBoxManual" Type="ComboBox" UpDownType="">4</Setting>
<Setting Name="Frame0_C1CounterTextBox" Type="UpDown" UpDownType="Hex">0015E0</Setting>
<Setting Name="Frame1_C1CounterTextBox" Type="UpDown" UpDownType="Hex">0015E0</Setting>
<Setting Name="Frame2_C1CounterTextBox" Type="UpDown" UpDownType="Hex">0015E0</Setting>
<Setting Name="Frame0_CtrlMsgTextBox" Type="UpDown" UpDownType="d">2</Setting>
<Setting Name="Frame1_CtrlMsgTextBox" Type="UpDown" UpDownType="d">2</Setting>
<Setting Name="Frame2_CtrlMsgTextBox" Type="UpDown" UpDownType="d">2</Setting>
<Setting Name="rxSeedBox" Type="UpDown" UpDownType="Hex">03777777</Setting>
<Setting Name="txSeedBox" Type="UpDown" UpDownType="Hex">03777777</Setting>
<Setting Name="forceRxSeed" Type="CheckBox" UpDownType="">False</Setting>
</Settings>
There a few hundred more settings but they all follow the same convention, this file is generated by the application I mentioned and layout has to be retained in order for the app to read it again.
What I want to do is:
1) Read the XML
2) Find fields with Name="rxSeedBox" and "txSeedBox" (Setting Names are unique)
3) Edit their values from 03777777 to something else, like 05FFFFFF and 08E243AF respectively
4) Save the modified xml so it can be loaded in the application
Here is the code so far:
import sys
import os
import time
import xml.etree.ElementTree as ET
from socket import * # portable socket interface plus constants
tree = ET.parse('txg.xml')
root = tree.getroot()
for Setting in root.findall('Setting'):
Name = Setting.get('Name')
Type = Setting.get('Type')
UpDownType = Setting.get('UpDownType')
print(Name, Type, UpDownType, Setting.text)
Pretty much all it does at this point is read the XML from the file and print its contents. I have no idea ho to search for specific, unique Name attributes and then change values. I tried with
value = tree.findtext('Setting')
commands but the only usage I've seen so far was to change the attributes.
I want the attributes to stay untouched but the values between tags to be changed. How do I do that with ElementTree?