示例代码说明:
遍历OD数据中每条道路Road,若Road中存在隧道tunnel属性,则将该道路ID和包含的所有隧道信息输出到xml文件中。
import xml.dom.minidom
from xml.dom.minidom import parse
from xml.dom import Node
import sys
import os
# 读取OpenDRIVE文件路径
xml_filepath = r'E:\OpenDRIVE.xodr'
# 得到文件对象
dom_obj = xml.dom.minidom.parse(xml_filepath)
# 得到xml文档对象
root = dom_obj.documentElement
roads = root.getElementsByTagName('road')
filepath = "D:\\tunnel.xml"
if (os.path.exists(filepath)):
os.remove(filepath)
fp = open(filepath, 'a+')
for road in roads:
tunnels = road.getElementsByTagName("tunnel")
if (len(tunnels) > 0):
print(road.getAttribute("id"))
fp.write((road.getAttribute("id")))
fp.write('\n')
for j in range(len(tunnels)):
print(tunnels[j].toxml())
fp.write(tunnels[j].toxml())
fp.write('\n')
fp.close()
OpenDRIVE文件示例:
xml输出结果示例: