Click here to Skip to main content
15,939,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 type inside the class ATTRIBUTE. I want just to display the values Demonstration and Portfolio in a text file

<ADOXML adoversion="Version 5.1" username="Admin" database="adoxxdb" time="10:39" date="21.10.2019" version="3.1">
<MODEL version="" applib="ADOxx 1.5 Dynamic Experimentation Library" libtype="bp" modeltype="Ressource Model" name="Ressource Model - new" id="mod.47204">
<INSTANCE name="Collection of written documents-49041" id="obj.49041" class="Collection of written documents">
<ATTRIBUTE name="Position" type="STRING">NODE x:7cm y:1.5cm index:1</ATTRIBUTE>
<ATTRIBUTE name="External tool coupling" type="STRING"/>
<ATTRIBUTE name="Comment" type="STRING"/>
<ATTRIBUTE name="Description" type="STRING"/>
<ATTRIBUTE name="Referenced Document" type="PROGRAMCALL">ITEM "" param:""</ATTRIBUTE>
<ATTRIBUTE name="Display file name" type="INTEGER">0</ATTRIBUTE>
<ATTRIBUTE name="Type of written documents" type="ENUMERATIONLIST">Demonstration;Portfolio</ATTRIBUTE>

The output should just look like:


My code looks like follows:

What I have tried:

import xml.etree.ElementTree as ET

tree=ET.parse(r"C:\Users\benni\Google Drive\MASTER\Masterarbeit\Coden\Resource.xml")

with open(r"C:\Users\benni\Google Drive\MASTER\Masterarbeit\Coden\Output88.txt", "w")as f:
    for instance in tree.findall(".//ATTRIBUTE"):

However, I do not get the results I wanted. I just get the attribute ENUMERATIONLIST, but I want the values of this.
Updated 20-Oct-19 23:43pm

1 solution

I just ran your code with the corrected* XML and it lists all the entries named ATTRIBUTE.

*the tag <MODELATTRIBUTES> does not have a corresponding closing tag, so I removed it from the file.
Share this answer
3razOr1993 21-Oct-19 6:34am    
Yes that is right, but I just need the values Demonstration and Portfolio as an endresult
Richard MacCutchan 21-Oct-19 6:40am    
Then parse for the values once you have found the correct node. See 19.7. xml.etree.ElementTree — The ElementTree XML API — Python 2.7.17 documentation[^] for further information.
3razOr1993 21-Oct-19 7:10am    
I read the further information but I still do not get it sorry.
My problem is I do not know how to get the values Demonstration and Portfolio, because I cannot get it with .find(xx).text or .get(xx)
Richard MacCutchan 21-Oct-19 7:17am    

if instance.get('type') == "ENUMERATIONLIST":

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900