使用python

时间:2017-06-09 11:32:32

标签: python xml

我想在xml文件中的任何MeasureSymbolGroup行之间插入一行代码。我有一些代码,但它无法正常工作。

我的代码:

tree = ET.parse(PATH_XML_FILE)
root = tree.getroot()
for sub in sorted(my_array):
    setSubValue = False
    for neighbor in root.iter('Command'):
        if "SWF" in str(neighbor.attrib):                
            if re.search(r"\b" + re.escape(sub) + r"\b", neighbor.text): # match whole word only
                setSubValue = True
                break

    if setSubValue == False:
        print sub
        f = ET.Element('Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true"' + sub +'\n')

        neighbor.insert(1, f)
tree.write(output.xml')

其中my_array是一个包含字符串列表的数组。

基础xml文件:

<?xml version="1.0" encoding="utf-8"?>
<T1Automation>
    <Title>AutomationTest</Title>
    <LogFilePath>..\Result\log.html</LogFilePath>
    <Script Name="Group Measurement" GuiVisible="true" ShowTraces="true" StoreTraces="true" ShutdownConnection="false">
        <Commands>
            <Command Cmd="PrintTime" />            

            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">aaa</Command>             
            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">bbb</Command> 
            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">ccc</Command>
            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">ddd</Command>          
            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">eee</Command>         
            <Command Cmd="DownloadMeasurement" LogIt="true"/>       
            <Command Cmd="PrintTime" />

        </Commands>
    </Script>
</T1Automation>

必需的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<T1Automation>
    <Title>AutomationTest</Title>
    <LogFilePath>..\Result\log.html</LogFilePath>
    <Script Name="Group Measurement" GuiVisible="true" ShowTraces="true" StoreTraces="true" ShutdownConnection="false">
        <Commands>
            <Command Cmd="PrintTime" />            

            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">aaa</Command>             
            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">bbb</Command> 
            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">ccc</Command>
            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">ddd</Command>     
            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">xxx</Command>             
            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">eee</Command>         
            <Command Cmd="DownloadMeasurement" LogIt="true"/>       
            <Command Cmd="PrintTime" />

        </Commands>
    </Script>
</T1Automation>

输出:

<?xml version="1.0" encoding="utf-8"?>
<T1Automation>
    <Title>AutomationTest</Title>
    <LogFilePath>..\Result\log.html</LogFilePath>
    <Script Name="Group Measurement" GuiVisible="true" ShowTraces="true" StoreTraces="true" ShutdownConnection="false">
        <Commands>
            <Command Cmd="PrintTime" />            

            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">aaa</Command>             
            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">bbb</Command> 
            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">ccc</Command>
            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">ddd</Command>          
            <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true">eee</Command>         
            <Command Cmd="DownloadMeasurement" LogIt="true"/>

            <Command Cmd="Print" Format="Bold">Test finished</Command>
            <Command Cmd="PrintTime">
                <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true"fff/>
                <Command Cmd="MeasureSymbolGroup" FlexType="SWF" LogIt="true"ggg/>
           </Command>
        </Commands>
    </Script>
</T1Automation>

0 个答案:

没有答案