1

I am working with a XML file that looks like the code below, the real one has a lot more spreekbeurt sessions but I made it readable. My goal is to get from all the spreekbeurt sessions the text in the voorvoegsel and achternaam part.

<?xml version="1.0" encoding="utf-8"?>
<officiele-publicatie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://technische-documentatie.oep.overheid.nl/schema/op-xsd-2012-2">
  <metadata>
    <meta name="OVERHEIDop.externMetadataRecord" scheme="" content="https://zoek.officielebekendmakingen.nl/h-tk-20122013-4-2/metadata.xml" />
  </metadata>

  <handelingen>

      <spreekbeurt nieuw="ja">
        <spreker>
          <voorvoegsels>De heer</voorvoegsels>
          <naam>
            <achternaam>Marcouch</achternaam>
          </naam> (<politiek>PvdA</politiek>):</spreker>
        <tekst status="goed">
          <al>Sample Text</al>
        </tekst>
      </spreekbeurt> 

    </agendapunt>
  </handelingen>
</officiele-publicatie>

I use a for loop to loop through all the spreekbeurt elemets in my XML file. But how do I print out the voorvoegsels and achternaam for every spreekbeurt in my XML file?

import xml.etree.ElementTree as ET
tree = ET.parse('...\directory')
root = tree.getroot()

for spreekbeurt in root.iter('spreekbeurt'):
    print spreekbeurt.attrib

This code prints:

{'nieuw': 'nee'}
{'nieuw': 'ja'}
{'nieuw': 'nee'}
{'nieuw': 'nee'}

but how do I get the children printed out of the spreekbeurt?

Thanks in advance!

1 Answer 1

1

You can use find() passing path* to the target element to find individual element within a parent/ancestor, for example :

>>> for spreekbeurt in root.iter('spreekbeurt'):
...     v = spreekbeurt.find('spreker/voorvoegsels')
...     a = spreekbeurt.find('spreker/naam/achternaam')
...     print v.text, a.text
...
De heer Marcouch

*) in fact it supports more than just simple path, but subset of XPath 1.0 expressions.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.