0
<people>
 <type>
  <name>lo_123</name>
  <country>AUS</country>
  <note>
     <name>joe</name>
     <age>26</age>
     <spouse>
         <name>lisa</name>
         <gender>f</gender>
     </spouse>     
  </note>
 </type>
<type>
  <name>hi_123</name>
  <country>AUS</country>
  <note>
     <name>paul</name>
     <gender>m</gender>
     <age>28</age>
     <spouse>
         <name>mona</name>
         <gender>f</gender>
     </spouse>     
  </note>
 </type>
</people>

I need to extract name that has gender like 'Paul'. But I dont know how to filter the 'child' that has gender.

Here is my code that I knew:

xml='xmltest.xml'

crif_tree = ET.parse(xml)
crif_root = crif_tree.getroot()

for a in _root.findall('./type/note/name'):
    print(a.text)
qwe
  • 27
  • 4

1 Answers1

1

try this crif_root.findall('./type/note[gender]/name'): it selects a note only if it has a child gender. in case gender needs to have a particular value, see How to select a node using XPath if sibling node has a specific value?

mrxra
  • 852
  • 1
  • 6
  • 9