I have the following xml data:
<?xml version="1.0"?>
<Company>
<Employee1>
<FirstName>Tanmay</FirstName>
<LastName>Patil</LastName>
<ContactNo>1234567890</ContactNo>
<Email>[email protected]</Email>
<Address>
<City>Bangalore</City>
</Address>
<name> XXXXX</name>
</Employee1>
<Employee2>
<FirstName>Tanmay</FirstName>
<LastName>Patil</LastName>
<ContactNo>1234567890</ContactNo>
<Email>[email protected]</Email>
<Address>
<City>Chennai</City>
</Address>
<name> YYYYYY</name>
</Employee2>
<Employee3>
<FirstName>Tanmay</FirstName>
<LastName>Patil</LastName>
<ContactNo>1234567890</ContactNo>
<Email>[email protected]</Email>
<Address>
<City>Bangalore</City>
</Address>
<name> ZZZZZ</name>
</Employee3>
</Company>
I want to filter based on, City = Bangalore and get relevant contents of name tags for each.
The desired output when filtered City = Bangalore:
<name> XXXXX</name>
<name> ZZZZZ</name>
I have tried using the below and nothing helped me:
import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
tree.findall('city=Bangalore').name
But did not get what I am trying. Can someone help please?
Try to use XPath: