I am having two issues with parsing an XML file. I want to only return one set of properties I.E only the property values under the first Process and I want to return the second Source under the second Process. When I use my code it returns the Source under the first Sources and the first Source under the second Sources but I cannot get the second Source to return.
The XML file looks like:
<!-- The description of the process -->
<Description>"This is a description"</Description>
<!-- info on process to be run -->
<Process expectFailure="false">
<Code>Import</Code>
<Sources>
<Source>"Test Data"</Source>
</Sources>
<Destination>Buffered</Destination>
<Properties>
<Property code="format" value="CC"/>
<Property code="Input" value="10N"/>
<Property code="Method" value="BASIC"/>
<Ppoperty code="Resolution" value="5"/>
<Property code="Convention" value="LEFT"/>
<Property code="Bounding" value="BUFFERED"/>
</Properties>
</Process>
<!-- info on second process to be run (compare) -->
<Process>
<Code>SurfaceCompare</Code>
<Sources>
<Source>expectedOutput</Source>
<Source>Buffered</Source>
</Sources>
<Properties>
<Property code="compare_designated" value="true"/>
<Property code="compare_metadata" value="true"/>
<Property code="metadata_type" value="OTHER"/>
</Properties>
</Process>
and the code looks like
from xml.etree import ElementTree
tree = ElementTree.parse("XML_example.xml")
description = tree.findtext("Description")
print(description)
for process in tree.findall('Process'):
for source in process.findall('Sources'):
source_text = source.findtext('Source')
print(source_text)
#returns everything
for property in process.iter('Property'):
print(property.attrib.get('code'))
print(property.attrib.get('value'))
for process in tree.findall('Process'):
for source in process.findall('Sources'):
source = source.findtext('Source')
print(source)
I've tried a lot of different ways of using the findall, find, iter, get, getiter methods. I am sure I am missing something but it has been a long day and for the life of me I can't see what I am missing.
There is also the ability to change how the XML is set up but I know there must be a way to solve this question and it is gnawing at me.
Sample proper output for sources:
"Test Data"
expectedOutput
buffered
Sample proper output 1 for properties:
format
CC
Input
10N
Method
BASIC
Convention
LEFT
Bounding
BUFFERED
Sample proper output 2:
compare_designated
true
compare_metadata
true
metadata_type
OTHER