2016-11-17 203 views
1

我有大約1000個xml文件,有些缺少元素。我製作了一個腳本來搜索xml並打印元素。我想增加添加元素的能力,如果它不存在,但是不成功。Python XML將元素添加到文件

正如您在下面看到的那樣,DVT3不存在,需要添加它。

我的代碼

XMLParser = etree.XMLParser(remove_blank_text=True) 
for f in os.listdir(directory): 
    if f.endswith(".xml"): 

     xmlfile = directory + '/' + f 


     tree = etree.parse(xmlfile, parser=XMLParser) 
     root = tree.getroot() 

     hardwareRevisionNode = root.find(".//hardwareRevision") 

     try: 
      print f + ' : ' + hardwareRevisionNode.text 
     except Exception as e: 
      print str(e) 
      print xmlfile 
      #Wearable = root.find(".//Wearable") 
      ChildNode = etree.Element(".//Wearable") 
      ChildNode.text = "DVT2" 
      ChildNode.append(ChildNode) 
      tree.write(xmlfile, pretty_print=True) 

XML文件

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> 
<Speech> 
    <Results> 
    <breakpoint name="ASR_START_RECOGNITION" elapsedTime="00:00:00.000" /> 
    </Results> 
    <Meta> 
    <Dialog> 
     <sessionUUID>7c9b1e3a-b22f-4793-818f-72bc6e7b84a9</sessionUUID> 
    </Dialog> 
    <ASR> 
     <engine> 
     <name>Rockhopper</name> 
     <version>1.0.0.61</version> 
     </engine> 
     <wrapper> 
     <name>RockhopperAsrEngine</name> 
     <version>1.8.2</version> 
     </wrapper> 
     <wrapper> 
     <name>Core</name> 
     <version>1.8.2</version> 
     </wrapper> 
     <resource> 
     <name>Language Model</name> 
     <version>1.4.4</version> 
     </resource> 
    </ASR> 
    <Application> 
     <name>FightClub</name> 
     <version>0.1.550</version> 
     <commit>8f7a411</commit> 
     <buildDate>2016-03-09T18:16Z</buildDate> 
     <branch>HEAD</branch> 
    </Application> 
    <Wearable> 
     <firmware>1.0.183 - FCB1APP000-1611W0183</firmware> 
    </Wearable> 
    </Meta> 
</Speech> 

XML文件,我想

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> 
<Speech> 
    <Results> 
    <breakpoint name="ASR_START_RECOGNITION" elapsedTime="00:00:00.000" /> 
    </Results> 
    <Meta> 
    <Dialog> 
     <sessionUUID>7c9b1e3a-b22f-4793-818f-72bc6e7b84a9</sessionUUID> 
    </Dialog> 
    <ASR> 
     <engine> 
     <name>Rockhopper</name> 
     <version>1.0.0.61</version> 
     </engine> 
     <wrapper> 
     <name>RockhopperAsrEngine</name> 
     <version>1.8.2</version> 
     </wrapper> 
     <wrapper> 
     <name>Core</name> 
     <version>1.8.2</version> 
     </wrapper> 
     <resource> 
     <name>Language Model</name> 
     <version>1.4.4</version> 
     </resource> 
    </ASR> 
    <Application> 
     <name>FightClub</name> 
     <version>0.1.550</version> 
     <commit>8f7a411</commit> 
     <buildDate>2016-03-09T18:16Z</buildDate> 
     <branch>HEAD</branch> 
    </Application> 
    <Wearable> 
     <firmware>1.0.183 - FCB1APP000-1611W0183</firmware> 
     <hardwareRevision>DVT3</hardwareRevision> 
    </Wearable> 
    </Meta> 
</Speech> 

回答

1

您可以嘗試xmltodict

import xmltodict as x 

with open(myfile) as f: 

    xmlDictionary=x.parse(f.read(),'utf-8') 
    xmlDictionary['Speech']['Meta']['Wearable'].update({"hardwareRevision": "DVT3"}) 

    output = x.unparse(xmlDictionary) 

    with open(outfile,'w') as out: 
     out.write(output) 

使它在並行,如果你想運行,如果存儲是一個問題簡單地替換文件的內容(或刪除新制成後immediatelly舊的)。