I have a dict mapping each xml tag to a dict key. I want to loop through each tag and text field in the xml, and compare it with the associated dict key value which is the key in another dict.
<2gMessage>
<Request>
<pid>daemon</pid>
<emf>123456</emf>
<SENum>2041788209</SENum>
<MM>
<MID>jbr1</MID>
<URL>http://jimsjumbojoint.com</URL>
</MM>
<AppID>reddit</AppID>
<CCS>
<Mode>
<SomeDate>true</CardPresent>
<Recurring>false</Recurring>
</Mode>
<Date>
<ASCII>B4788250000028291^RRR^15121015432112345601</ASCII>
</Date>
<Amount>100.00</Amount>
</CCS>
</Request>
</2gMessage>
The code I have so far:
parser = etree.XMLParser(ns_clean=True, remove_blank_text=True)
tree = etree.fromstring(strRequest, parser)
for tag in tree.xpath('//Request'):
subfields = tag.getchildren()
for subfield in subfields:
print (subfield.tag, subfield.text)
return strRequest
But, this only prints the tags which are direct children of Request, I want to be able to access the subchildren on children if it is an instance in the same loop. I don't want to hardcode values, as the tags and structure could be changed.
xmlis also bad formed.