0

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?

Mark
  • 3
  • 2
  • I assume there's something else in the file that is non-standard as that xml fragment in your example looks perfectly valid. – Phylogenesis Feb 17 '17 at 13:40
  • Hmmm, maybe it is a regular XML then. If you say it's perfectly valid, I'll change the description. There is nothing else in that file, just more of the – Mark Feb 17 '17 at 13:45
  • There's nothing non-standard about it. – Michael Kay Feb 17 '17 at 20:40

1 Answers1

0

What you have proposed seems to work fine. Just find the one that you want to change and change it:

import sys
import os
import time
import xml.etree.ElementTree as ET

txg = """
    <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>
"""

tree = ET.ElementTree(ET.fromstring(txg))
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)

print

for Setting in root.findall('Setting'):
    if Setting.get('Name') == 'rxSeedBox':
      Setting.text = '05FFFFFF'

for Setting in root.findall('Setting'):
    Name = Setting.get('Name')
    Type = Setting.get('Type')
    UpDownType = Setting.get('UpDownType')
    print(Name, Type, UpDownType, Setting.text)

You get the following output

('Frame0_TypeTextBox', 'UpDown', 'd', '1')
('Frame1_TypeTextBox', 'UpDown', 'd', '2')
('Frame2_TypeTextBox', 'UpDown', 'd', '1')
('messageTypeBoxManual', 'ComboBox', '', '4')
('Frame0_C1CounterTextBox', 'UpDown', 'Hex', '0015E0')
('Frame1_C1CounterTextBox', 'UpDown', 'Hex', '0015E0')
('Frame2_C1CounterTextBox', 'UpDown', 'Hex', '0015E0')
('Frame0_CtrlMsgTextBox', 'UpDown', 'd', '2')
('Frame1_CtrlMsgTextBox', 'UpDown', 'd', '2')
('Frame2_CtrlMsgTextBox', 'UpDown', 'd', '2')
('rxSeedBox', 'UpDown', 'Hex', '03777777')
('txSeedBox', 'UpDown', 'Hex', '03777777')
('forceRxSeed', 'CheckBox', '', 'False')

('Frame0_TypeTextBox', 'UpDown', 'd', '1')
('Frame1_TypeTextBox', 'UpDown', 'd', '2')
('Frame2_TypeTextBox', 'UpDown', 'd', '1')
('messageTypeBoxManual', 'ComboBox', '', '4')
('Frame0_C1CounterTextBox', 'UpDown', 'Hex', '0015E0')
('Frame1_C1CounterTextBox', 'UpDown', 'Hex', '0015E0')
('Frame2_C1CounterTextBox', 'UpDown', 'Hex', '0015E0')
('Frame0_CtrlMsgTextBox', 'UpDown', 'd', '2')
('Frame1_CtrlMsgTextBox', 'UpDown', 'd', '2')
('Frame2_CtrlMsgTextBox', 'UpDown', 'd', '2')
('rxSeedBox', 'UpDown', 'Hex', '05FFFFFF')
('txSeedBox', 'UpDown', 'Hex', '03777777')
('forceRxSeed', 'CheckBox', '', 'False')

Then, it could be than when your print it to xml format you are changing the format. This can sometimes happen with ElementTree. You can look at ways to print this in the format that you preper for example in Pretty printing XML in Python or use xml.etree.elementtree to write out nicely formatted xml files [duplicate]

Community
  • 1
  • 1
sebas
  • 869
  • 7
  • 16
  • Thanks, this works just the way I wanted. I'm still relatively new to Python so not everything is as obvious as I hope it will one day become. – Mark Feb 17 '17 at 14:13