Python和XML解析,修改,保存

时间:2017-02-17 13:35:06

标签: python xml elementtree

我有一个xml文件,其中包含控制设备的GUI应用程序的默认设置:

    <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>

还有几百个设置,但它们都遵循相同的约定,这个文件由我提到的应用程序生成,并且必须保留布局,以便应用程序再次读取它。

我想做的是:

1)阅读XML

2)使用Name =&#34; rxSeedBox&#34;查找字段和&#34; txSeedBox&#34; (设置名称是唯一的)

3)将其值从03777777编辑为其他值,分别为05FFFFFF和08E243AF

4)保存修改后的xml,以便可以在应用程序中加载

以下是目前的代码:

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)

此时它所做的几乎就是从文件中读取XML并打印其内容。 我不知道如何搜索特定的,唯一的Name属性,然后更改值。 我试过

value = tree.findtext('Setting') 

命令,但我到目前为止看到的唯一用法是更改属性。

我希望属性保持不变,但要更改标记之间的值。 我如何使用ElementTree做到这一点?

1 个答案:

答案 0 :(得分:0)

你提出的建议似乎工作正常。只需找到您想要更改的那个并进行更改:

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)

您获得以下输出

('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')

然后,它可能比您将其打印为xml格式时更改格式。 ElementTree有时会发生这种情况。您可以查看以您预先制作的格式打印此内容的方式,例如Pretty printing XML in Pythonuse xml.etree.elementtree to write out nicely formatted xml files [duplicate]