0

I am trying to parse XML file, but get a little bit confused with namespaces and tags. At the end I need to get values from

  • d:Title
  • d:Date
  • d:Day
  • d:Night
  • d:Accuweather
  • d:Gismeteo
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xml:base="https://portal.erg.kz/weather/_api/">
       <id>0832f8fd-2ca1-4152-877b-3b28fc3eb1dc</id>
       <title />
       <updated>2020-02-12T08:53:05Z</updated>
       <entry m:etag="&quot;385&quot;">
          <id>Web/Lists(guid\'891430d2-9610-455c-be63-aa1bfd3c482f\')/Items(1)</id>
          <category term="SP.Data.WeatherInfoListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
          <link rel="edit" href="Web/Lists(guid\'891430d2-9610-455c-be63-aa1bfd3c482f\')/Items(1)" />
          <title />
          <updated>2020-02-12T08:53:05Z</updated>
          <author>
             <name />
          </author>
          <content type="application/xml">
             <m:properties>
                <d:Title>Лисаковск</d:Title>
                <d:Date m:type="Edm.DateTime">2020-02-12T00:00:00Z</d:Date>
                <d:Day m:type="Edm.Double">-8</d:Day>
                <d:Night m:type="Edm.Double">-14</d:Night>
                <d:Accuweather>Теплее</d:Accuweather>
                <d:Gismeteo>Переменная облачность, небольшой снег</d:Gismeteo>
                <d:Intellicast m:null="true" />
                <d:theWeatherChannel m:null="true" />
             </m:properties>
          </content>
       </entry>
...

The following code returns nothing

url = 'https://portal.site.com/weather/_api/web/lists#'
r = requests.get(url, auth=HttpNtlmAuth('user','pass'))
xml_string = r.text
root = ET.fromstring(xml_string)
root.findall('{http://schemas.microsoft.com/ado/2007/08/dataservices/metadata}m:properties')

Can somebody helps me to deal with the code please. Thanks!

3
  • what error do you get? Commented Feb 12, 2020 at 10:44
  • there is no error, just i got empty list Commented Feb 12, 2020 at 10:58
  • Looks like (I haven't tested) you just need to remove the m: prefix in your findall...root.findall('{http://schemas.microsoft.com/ado/2007/08/dataservices/metadata}properties'). Commented Feb 12, 2020 at 16:06

1 Answer 1

1

You can create a dict namespace of aliases for easier xpath queries

from xml.etree.cElementTree import ElementTree

tree = ElementTree(file="file.xml")
root = tree.getroot()
namespaces = {
    "p": "http://www.w3.org/2005/Atom",
    "m": "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata",
    "d": "http://schemas.microsoft.com/ado/2007/08/dataservices",
}
xpath = "p:entry/p:content/m:properties"
res = root.findall(xpath, namespaces=namespaces)

values = []

for x in res:
    values.append(
        {
            "Title": x.find("d:Title", namespaces=namespaces).text,
            "Date": x.find("d:Date", namespaces=namespaces).text,
            "Day": x.find("d:Day", namespaces=namespaces).text,
            "Night": x.find("d:Night", namespaces=namespaces).text,
            "Accuweather": x.find("d:Accuweather", namespaces=namespaces).text,
            "Gismeteo": x.find("d:Gismeteo", namespaces=namespaces).text,
        }
    )
print(values)

Prints

[
    {
        "Title": "Лисаковск",
        "Date": "2020-02-12T00:00:00Z",
        "Day": "-8",
        "Night": "-14",
        "Accuweather": "Теплее",
        "Gismeteo": "Переменная облачность, небольшой снег",
    }
]
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.