from ftplib import FTP
import xml.etree.ElementTree as ET
import pandas as pd
def parse_referat(filename):
# Parse the XML content
print("Parsing XML content...")
tree = ET.parse(filename)
root = tree.getroot()
code
# Initialize data list
data = []
# Process each DagsordenPunkt
for dagsorden in root.findall('.//DagsordenPunkt'):
# Get MetaFTAgendaItem information
meta_ft = dagsorden.find('.//MetaFTAgendaItem')
if meta_ft is not None:
# Extract required fields
item_no = meta_ft.find('ItemNo')
ft_case = meta_ft.find('FTCase')
ft_case_type = meta_ft.find('FTCaseType')
short_title = meta_ft.find('ShortTitle')
# Create dictionary for this DagsordenPunkt
entry = {
'ItemNo': item_no.text if item_no is not None else '',
'FTCase': ft_case.attrib.get('tingdokID', '') if ft_case is not None else '',
'FTCaseType': ft_case_type.text if ft_case_type is not None else '',
'ShortTitle': short_title.text if short_title is not None else ''
}
# Add speakers and their speeches
speakers = []
speeches = []
for talk in dagsorden.findall('.//Tale'):
# Get speaker information
speaker_elem = talk.find('.//MetaSpeakerMP')
if speaker_elem is None:
speaker_elem = talk.find('.//Taler')
if speaker_elem:
first_name = speaker_elem.find('OratorFirstName')
last_name = speaker_elem.find('OratorLastName')
party = speaker_elem.find('GroupNameShort')
speaker = f"{first_name.text} {last_name.text}" if first_name is not None and last_name is not None else ''
if party is not None:
speaker += f" ({party.text})"
speakers.append(speaker)
# Get speech content
speech = []
for segment in talk.findall('.//TekstGruppe'):
for exitus in segment.findall('.//Exitus'):
for linea in exitus.findall('.//Linea'):
char_elem = linea.find('Char')
if char_elem is not None and char_elem.text is not None:
speech.append(char_elem.text.strip())
speeches.append('\n'.join(speech))
# Add speakers and speeches to entry
entry['Speakers'] = ', '.join(speakers)
entry['Speeches'] = '\n\n'.join(speeches)
data.append(entry)
# Create DataFrame
df = pd.DataFrame(data)
# Set display options for better viewing
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)
pd.set_option('display.width', None)
return df
Assistant
To incorporate the meeting date from the DateOfSitting element in the XML document into the resulting DataFrame, we need to extract that value from the MetaMeeting section at the root level of the XML.
✅ Key Steps:
Locate the MetaMeeting Element:
This is a child of the root <Dokument>, and contains the <DateOfSitting> field.
Extract the Date:
The DateOfSitting contains a full ISO 8601 datetime string like 2024-05-28T13:00:00.
We extract only the date portion (YYYY-MM-DD) by splitting on 'T'.
Add the Date to Each Row:
Since the date is the same for all agenda items in the meeting, we store it once and append it to every row in the resulting DataFrame.
✅ Updated Code:
python
from ftplib import FTP
import xml.etree.ElementTree as ET
import pandas as pd
def parse_referat(filename):
# Parse the XML content
print("Parsing XML content...")
tree = ET.parse(filename)
root = tree.getroot()
# Extract meeting date from MetaMeeting
meeting_date = ''
meta_meeting = root.find('MetaMeeting') # Direct child of root
if meta_meeting is not None:
date_elem = meta_meeting.find('DateOfSitting')
if date_elem is not None and date_elem.text:
# Extract just the date part
meeting_date = date_elem.text.split('T')[0]
# Initialize data list
data = []
# Process each DagsordenPunkt
for dagsorden in root.findall('.//DagsordenPunkt'):
# Get MetaFTAgendaItem information
meta_ft = dagsorden.find('.//MetaFTAgendaItem')
if meta_ft is not None:
# Extract required fields
item_no = meta_ft.find('ItemNo')
ft_case = meta_ft.find('FTCase')
ft_case_type = meta_ft.find('FTCaseType')
short_title = meta_ft.find('ShortTitle')
# Create dictionary for this DagsordenPunkt
entry = {
'ItemNo': item_no.text if item_no is not None else '',
'FTCase': ft_case.attrib.get('tingdokID', '') if ft_case is not None else '',
'FTCaseType': ft_case_type.text if ft_case_type is not None else '',
'ShortTitle': short_title.text if short_title is not None else '',
'MeetingDate': meeting_date # Add the extracted date here
}
# Collect speakers and speeches
speakers = []
speeches = []
for talk in dagsorden.findall('.//Tale'):
speaker_elem = talk.find('.//MetaSpeakerMP')
if speaker_elem is None:
speaker_elem = talk.find('.//Taler')
if speaker_elem:
first_name = speaker_elem.find('OratorFirstName')
last_name = speaker_elem.find('OratorLastName')
party = speaker_elem.find('GroupNameShort')
speaker = f"{first_name.text} {last_name.text}" if first_name is not None and last_name is not None else ''
if party is not None:
speaker += f" ({party.text})"
speakers.append(speaker)
# Get speech content
speech = []
for segment in talk.findall('.//TekstGruppe'):
for exitus in segment.findall('.//Exitus'):
for linea in exitus.findall('.//Linea'):
char_elem = linea.find('Char')
if char_elem is not None and char_elem.text:
speech.append(char_elem.text.strip())
speeches.append('\n'.join(speech))
# Add to entry
entry['Speakers'] = ', '.join(speakers)
entry['Speeches'] = '\n\n'.join(speeches)
data.append(entry)
# Create DataFrame
df = pd.DataFrame(data)
# Set display options for better viewing
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)
pd.set_option('display.width', None)
return df
✅ Result:
Each row in the returned DataFrame will now include a new column:
Column Name
Description
MeetingDate
The date of the meeting in YYYY-MM-DD format
📌 Notes:
This solution assumes that the XML structure is consistent and that DateOfSitting is always present.
If needed, you can enhance error handling by adding try/except blocks or validating the XML structure further.
If namespaces were in use, you'd need to pass a namespace dictionary to find() and findall(), but based on the provided example, they are not used.