1

I'm trying to parse following xml file with xml.etree. However, it does't produce any results.

from xml.etree import cElementTree as ET
xmlstr = """<?xml version='1.0' encoding='UTF-8'?>

<tns:getCamerasResponse xmlns:tns="https://infoconnect.highwayinfo.govt.nz/schemas/camera2">

    <tns:camera>

        <tns:description>North along Sth Wstn Mwy from May Rd</tns:description>

        <tns:direction>Northbound</tns:direction>

        <tns:group>NA</tns:group>

        <tns:id>653</tns:id>

        <tns:imageUrl>http://www.trafficnz.info/camera/653.jpg</tns:imageUrl>

        <tns:lat>-36.90943</tns:lat>

        <tns:lon>174.73442</tns:lon>

        <tns:name>SH20 May Rd Overbridge</tns:name>

        <tns:offline>false</tns:offline>

        <tns:region>Auckland</tns:region>

        <tns:thumbUrl>http://www.trafficnz.info/camera/thumb/653.jpg</tns:thumbUrl>

        <tns:underMaintenance>false</tns:underMaintenance>

        <tns:viewUrl>http://www.trafficnz.info/camera/view/653</tns:viewUrl>

 </tns:camera>
 </tns:getCamerasResponse>"""



root = ET.fromstring(xmlstr)


results = root.findall('tns:camera', {'tns':"https://infoconnect.highwayinfo.govt.nz/schemas/camera2"})
for camera in results:
    region = camera.find('tns:region')
    if region is not None:
        print(region.text)
5
  • 1
    You are missing the namespace I think results = root.findall('ns:camera', {'ns':"https://infoconnect.highwayinfo.govt.nz/schemas/camera2"}) should work Commented Sep 24, 2019 at 22:52
  • 1
    The tag in question isn't <camera> -- it's <tns:camera>, and you aren't accounting for that. Read up on xml namespaces. Commented Sep 24, 2019 at 22:53
  • @Manuel: added this "results = root.findall('tns:camera', {'tns':"infoconnect.highwayinfo.govt.nz/schemas/camera2"})". Still giving empty results. Commented Sep 24, 2019 at 22:58
  • results = root.findall('tns:camera', {'tns':"https://infoconnect.highwayinfo.govt.nz/schemas/camera2"}) works for me Commented Sep 24, 2019 at 23:00
  • @Manuel: I have edited the code after adding the namespace. Still I'm not getting any results. Could please post your code as an answer? Thank you. Commented Sep 24, 2019 at 23:05

1 Answer 1

1

You need to account for namespaces in your find methods.

from xml.etree import cElementTree as ET
xmlstr = """<?xml version='1.0' encoding='UTF-8'?>

<tns:getCamerasResponse xmlns:tns="https://infoconnect.highwayinfo.govt.nz/schemas/camera2">

    <tns:camera>

        <tns:description>North along Sth Wstn Mwy from May Rd</tns:description>

        <tns:direction>Northbound</tns:direction>

        <tns:group>NA</tns:group>

        <tns:id>653</tns:id>

        <tns:imageUrl>http://www.trafficnz.info/camera/653.jpg</tns:imageUrl>

        <tns:lat>-36.90943</tns:lat>

        <tns:lon>174.73442</tns:lon>

        <tns:name>SH20 May Rd Overbridge</tns:name>

        <tns:offline>false</tns:offline>

        <tns:region>Auckland</tns:region>

        <tns:thumbUrl>http://www.trafficnz.info/camera/thumb/653.jpg</tns:thumbUrl>

        <tns:underMaintenance>false</tns:underMaintenance>

        <tns:viewUrl>http://www.trafficnz.info/camera/view/653</tns:viewUrl>

 </tns:camera>
 </tns:getCamerasResponse>"""



root = ET.fromstring(xmlstr)

ns = {'tns':"https://infoconnect.highwayinfo.govt.nz/schemas/camera2"}
results = root.findall('tns:camera', ns)
for camera in results:
    region = camera.find('tns:region', ns)
    if region is not None:
        print(region.text)

If you need to know how to get the namespace, this could help maybe Python: ElementTree, get the namespace string of an Element

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.