Find all Revision
tags, print all attributes from element.attrib
, iterate over the Revision
element to get the children and Name
attribute values:
import xml.etree.ElementTree as etree
data = """<?xml version="1.0" encoding="UTF-8"?>
<VersionedFile EntityPath="c:\\a.zip" Name="VfOMP_CRM.zip">
<WorkfileDescription>something</WorkfileDescription>
<Revision EntityPath="c:\\a.zip" Name="1.1" Author="me">
<ChangeDescription>Some comentary</ChangeDescription>
<PGROUP Name="A" />
<PGROUP Name="B" />
<PGROUP Name="C" />
<Label Name="SOFTWARE" />
<Label Name="READY" />
</Revision>
<Revision EntityPath="c:\\a.zip" Name="1.0" Author="me">
<ChangeDescription>Some comentary</ChangeDescription>
<PGROUP Name="A" />
<Label Name="GAME" />
<Label Name="READY" />
</Revision>
</VersionedFile>
"""
tree = etree.fromstring(data)
for revision in tree.findall('Revision'):
for key, value in revision.attrib.iteritems():
print "%s: %s" % (key, value)
for child in revision:
print "%s: %s" % (child.tag, child.attrib.get('Name', ''))
print
prints:
Name: 1.1
EntityPath: c:\a.zip
Author: me
ChangeDescription:
PGROUP: A
PGROUP: B
PGROUP: C
Label: SOFTWARE
Label: READY
Name: 1.0
EntityPath: c:\a.zip
Author: me
ChangeDescription:
PGROUP: A
Label: GAME
Label: READY
You may need to tweak it a bit to have the desired output, but this should give you the basic idea.