0

I wrote a code in VBA for Excel that store in a cell data from a XML like this (https://nominatim.openstreetmap.org/reverse?lat=45.387838002000002&lon=7.6777959990000104) part of the code is:

'carichiamo la risposta
    xDoc.Load (url)
    'prendiamo le componenti della risposta
    Set postcode = xDoc.SelectSingleNode("/reversegeocode/addressparts/postcode")

Now I'm trying to translate that function to PyQGIS. What I want is to store the same info to specific field but I can't find a way to get information from this XML.

1
  • 1
    I would make use of lxml
    – Ian Turton
    Commented Jun 21, 2023 at 9:46

1 Answer 1

1

As @IanTurton suggests, I would also make use of lxml, however it's not in the standard library, so here's a way using xml.etree.ElementTree which is in the standard library.

from urllib.request import urlopen
import xml.etree.ElementTree as et

with urlopen('https://nominatim.openstreetmap.org/reverse?lat=45.387838002000002&lon=7.6777959990000104') as f:
    tree = et.parse(f)
    postcode = tree.find("./addressparts/postcode").text

1
  • Thank for your answer. I didn't test it because i found another way that works well: for f in newLyr2.selectedFeatures(): context.setFeature(f) ricerca = '">(.+?)</result>' urlRich = expr1.evaluate(context) if re.search(ricerca, str(urlopen(urlRich).read())) is None: txt ='' else: txt =re.search(ricerca, str(urlopen(urlRich).read())).group(1).split('">')[1] attr_map[f.id()] = {newLyr2.fields().indexFromName("B2full"): txt} pass
    – freecma80
    Commented Aug 31, 2023 at 10:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.