theamokdog
theamokdog

Reputation: 39

Parsing XML with Python - namespaces

I want to work with open data, the xml is stored here:

http://offenedaten.frankfurt.de/dataset/912fe0ab-8976-4837-b591-57dbf163d6e5/resource/48378186-5732-41f3-9823-9d1938f2695e/download/parkdatendyn.xml

with help from this forum i wrote the code

from lxml import etree
from urllib.request import urlopen


root = etree.parse(urlopen(url)).getroot()


ns = { 'd': 'http://datex2.eu/schema/2/2_0' }

parking_area = root.xpath('//d:parkingAreaStatus', namespaces=ns)
parking_facility = root.xpath('//d:parkingFacilityStatus', namespaces=ns)

for pa in parking_area:
    area_ref = pa.find('d:parkingAreaReference', ns)
    ParkingspaceList.append(str((area_ref.get('id'))))


for pf in parking_facility:
    facility_ref = pf.find('d:parkingFacilityReference', ns)
    ParkingspaceList.append(str((facility_ref.get('id'))))

It seems like this works for "pa" but for "pf" i got a failure message:

ParkingspaceList.append(str((facility_ref.get('id')))) AttributeError: 'NoneType' object has no attribute 'get'

Any Tips?

best regards

TR

Upvotes: 0

Views: 139

Answers (1)

Jesper
Jesper

Reputation: 1659

There are parkingFacilityStatus elements inside the identically named parkingFacilityStatus elements, which is rather confusing:

<parkingFacilityStatus>
    …
    <parkingFacilityReference targetClass="ParkingFacility" id="24278[Karstadt]" version="1.0"/>
    <parkingFacilityStatus>closed</parkingFacilityStatus>
    …
</parkingFacilityStatus>

It's the

    <parkingFacilityStatus>closed</parkingFacilityStatus>

that gives the error, as that element does not have a parkingFacilityReference subelement.

Use

for pf in parking_facility:
    facility_ref = pf.find('d:parkingFacilityReference', ns)
    if facility_ref is not None:
        ParkingspaceList.append(str((facility_ref.get('id'))))

and it works.

Upvotes: 1

Related Questions