0

How i can access Car from box subelement. I am able to get the attribute name but facing issue to get the Car

<annotations>
      <image height="940" id="0" name="90.jpg" width="1820">
        <box label="Objects" occluded="1" xbr="255" xtl="0" ybr="624" ytl="509">
         <attribute name="Class">Car</attribute>
         <attribute name="Occlusion %">25-50%</attribute>
         <attribute name="Truncation %">0%</attribute>
        </box>
      </image>
    </annotations>
Sanjay
  • 107
  • 1
  • 4
  • 17
  • Does this answer your question? [How do I parse XML in Python?](https://stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python) – buran Mar 14 '20 at 09:48
  • If you are "able to get the attribute name" then please show the code you have so far, and show the output you expect. Thanks. https://stackoverflow.com/help/minimal-reproducible-example – Mathias Müller Mar 14 '20 at 10:02

1 Answers1

2

See below

import xml.etree.ElementTree as ET


xml = '''<annotations>
      <image height="940" id="0" name="90.jpg" width="1820">
        <box label="Objects" occluded="1" xbr="255" xtl="0" ybr="624" ytl="509">
         <attribute name="Class">Car</attribute>
         <attribute name="Occlusion %">25-50%</attribute>
         <attribute name="Truncation %">0%</attribute>
        </box>
      </image>
    </annotations>'''

#
# Find the attribute element (under box element) where the attribute name value is 'Class'. print the text of the element text
#
root = ET.fromstring(xml)
print(root.find(".//box/attribute[@name='Class']").text)
balderman
  • 22,927
  • 7
  • 34
  • 52