I want to parse a xml file and save it as a txt file.
My XML-File looks like follows:
I am just interested in the attribute class inside INSTANCE
<ADOXML adoversion="Version 5.1" username="Admin" database="adoxxdb" time="09:49" date="18.09.2019" version="3.1">
<MODELS>
<MODEL version="" applib="ADOxx 1.5 Dynamic Experimentation Library" libtype="bp" modeltype="DSML4VPL" name="DSML4VPL - new (2)" id="mod.29201">
<INSTANCE name="Online entry point-42200" id="obj.42200" class="Online entry point">
<ATTRIBUTE name="Position" type="STRING">NODE x:2cm y:4cm index:1</ATTRIBUTE>
<ATTRIBUTE name="External tool coupling" type="STRING"/>
</INSTANCE>
<INSTANCE name="Interact-42206" id="obj.42206" class="**Interact**">
<ATTRIBUTE name="Position" type="STRING">NODE x:7.5cm y:4cm index:2</ATTRIBUTE>
<ATTRIBUTE name="External tool coupling" type="STRING"/>
<ATTRIBUTE name="Comment" type="STRING"/>
<ATTRIBUTE name="Description" type="STRING"/>
<ATTRIBUTE name="Open Questions" type="STRING"/>
</INSTANCE>
<INSTANCE name="Select-42210" id="obj.42210" class="**Select**">
<ATTRIBUTE name="Position" type="STRING">NODE x:12.5cm y:4cm index:4</ATTRIBUTE>
<ATTRIBUTE name="External tool coupling" type="STRING"/>
<ATTRIBUTE name="Comment" type="STRING"/>
<ATTRIBUTE name="Description" type="STRING"/>
<ATTRIBUTE name="Open questions" type="STRING"/>
</INSTANCE>
</MODEL>
</MODELS>
</ADOXML>
I just want to write every class like "Online entry point" or "Interact" inside a txt.file
The output should just look like
Klassen
Online entry point
Interact
Select
My code looks like follows:
import xml.etree.ElementTree as ET
tree=ET.parse("test1.xml")
root=tree.getroot()
with open("file3.txt","w")as f:
f.write("Class\n")
for xclass in root.findall("MODEL"):
Klasse=xclass.find("INSTANCE").get("class")
line_to_write=Klasse
with open("file3.txt","a") as f:
f.write(line_to_write)
However, I do not now what I am making wrong, there is no error message, just a txt.file with Class in it.