vincentlai
vincentlai

Reputation: 429

Parsing XML in Python finding element

I have a xml file (a1.xml).

  <object>
    <label id="0">Helmet</label>
    <bndbox id="0">
      <xmin>295</xmin>
      <ymin>304</ymin>
    </bndbox>

    <label id="1">Person</label>
    <bndbox id="1">
      <xmin>279</xmin>
      <ymin>291</ymin>
    </bndbox>
  </object>

I want to get this format

obj = [{'label': 'Helmet', 'xmin': 295, 'ymin': 304},
       {'label': 'Person', 'xmin': 279, 'ymin': 291}]

my code as following

import xml.etree.cElementTree as ET
tree = ET.parse('a1.xml')
for subtag in root.findall('object'):
    tag = subtag.findall('label').text
    x = subtag.findall('xmin').text
    y = subtag.findall('ymin').text

How to solve this?

Upvotes: 0

Views: 56

Answers (1)

Erik Wolf
Erik Wolf

Reputation: 84

Maybe using XPath syntax helps here. Clean coding would also check if the find()-results are not None, e.g.

import xml.etree.cElementTree as ET
tree = ET.parse('a1.xml')
labels = tree.findall('.//label')
for label in labels:
    label_id = label.get('id')
    # find the first bndbox with id set to the same value
    bndbox = tree.find('.//bndbox[@id="%s"]' % label_id)
    if bndbox is None:
        continue
    tag = label.text
    if bndbox.find('xmin') and bndbox.find('ymin'):
        x = bndbox.find('xmin').text
        y = bndbox.find('ymin').text
    else:
        continue
    # here you can store tag, x, y in a list or dictionary

Upvotes: 1

Related Questions