我有一个包含 XML 的字符串变量:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<osm attribution="http://www.openstreetmap.org/copyright" copyright="OpenStreetMap and contributors" generator="openstreetmap-cgimap 2.0.1 (3329554 spike-07.openstreetmap.org)" license="http://opendatacommons.org/licenses/odbl/1-0/" version="0.6">
<way changeset="123350178" id="26695601" timestamp="2022-07-08T08:32:16Z" uid="616103" user="Max Tenerelli" version="12" visible="true">
<nd ref="289140256"/>
<nd ref="292764243"/>
<nd ref="291616556"/>
<nd ref="292764242"/>
<nd ref="291616560"/>
<nd ref="291616561"/>
<nd ref="291616562"/>
<tag k="access" v="permissive"/>
<tag k="highway" v="service"/>
<tag k="maxspeed" v="30"/>
<tag k="name" v="Baracconi - Jacotenente"/>
<tag k="oneway" v="no"/>
<tag k="surface" v="paved"/>
</way>
</osm>
我需要nd
使用 Python 读取所有节点(引用值)。我编写了以下代码,但不起作用:
import xml.etree.ElementTree as ET
root = ET.fromstring(data)
for eir in root.findall('nodes'):
print(eir.text)
way
元素并搜索nd
内部nodes
,而是搜索元素名称:nd
。.text
,要获取属性使用:eir.attrib['ref']
输出:
在线尝试一下!