使用 Python xml.etree 解析 xml 文件:空结果
Parsing xml files with Python xml.etree : empty results
我正在尝试用 xml.etree
解析以下 xml 文件。但是,它不会产生任何结果。
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)
您需要在查找方法中考虑命名空间。
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)
如果您需要知道如何获取命名空间,这可能会有所帮助
Python: ElementTree, get the namespace string of an Element
我正在尝试用 xml.etree
解析以下 xml 文件。但是,它不会产生任何结果。
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)
您需要在查找方法中考虑命名空间。
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)
如果您需要知道如何获取命名空间,这可能会有所帮助 Python: ElementTree, get the namespace string of an Element