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>