python - 如何在python中创建具有多个元素的xml文件
问题描述
我想使用 python 创建一个 XML 文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<vehicle id="m0">
<timestep pos="2.3000" angle="11.1766" lane="-250709918#7_0" speed="0.0000" time="8.0"
</vehicle>
<vehicle id="m1">
<timestep pos="2.3000" angle="11.1766" lane="-250709918#7_0" speed="0.0000" time="8.0"
</vehicle>
........
我的代码:
doc = xml.dom.minidom.Document()
root = doc.createElement('vehicle')
for veh in veh_dict:
root.setAttribute('id', veh)
doc.appendChild(root)
for index, value in enumerate(veh_dict[veh]):
nodeManager = doc.createElement('timestep')
nodeManager.setAttribute('time', str(veh_dict[veh][index]['time']))
nodeManager.setAttribute('angle', str(veh_dict[veh][index]['angle']))
nodeManager.setAttribute('lane', str(veh_dict[veh][index]['lane']))
nodeManager.setAttribute(' pos', str(veh_dict[veh][index]['pos']))
nodeManager.setAttribute('speed', str(veh_dict[veh][index]['speed']))
nodeManager.setAttribute('type', str(veh_dict[veh][index]['type']))
nodeManager.setAttribute('x', str(veh_dict[veh][index]['x']))
nodeManager.setAttribute('y', str(veh_dict[veh][index]['y']))
root.appendChild(nodeManager)
fp = open('Manager.xml', 'w')
doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
我的输出包含所有数据,但它们都写在这样的“车辆”之一中:
<vehicle id="m2.9">
<timestep pos="2.3000" angle="11.1766" lane="-250709918#7_0" speed="0.0000" time="8.0" type="custom_moto" x="469.2605" y="5896.8761"/>
<timestep pos="3.3001" angle="12.9664" lane="-250709918#7_0" speed="1.0001" time="9.0" type="custom_moto" x="470.1134" y="5907.0132"/>
<timestep pos="6.4467" angle="12.2144" lane="-250709918#7_0" speed="3.1466" time="10.0" type="custom_moto" x="470.849" y="5900.3489"/>
<timestep pos="12.7147" angle="11.8696" lane="-250709918#7_0" speed="6.2681" time="11.0"
.......
根目录总是被覆盖吗?怎么解决?
解决方案
在循环内添加根元素:
import xml.dom.minidom
doc = xml.dom.minidom.Document()
topElem = doc.createElement('vehicles')
for veh in veh_dict:
for index, value in enumerate(veh_dict[veh]):
root = doc.createElement('vehicle')
root.setAttribute('id', veh)
doc.appendChild(root)
nodeManager = doc.createElement('timestep')
nodeManager.setAttribute('time', str(veh_dict[veh][index]['time']))
nodeManager.setAttribute('angle', str(veh_dict[veh][index]['angle']))
nodeManager.setAttribute('lane', str(veh_dict[veh][index]['lane']))
nodeManager.setAttribute(' pos', str(veh_dict[veh][index]['pos']))
nodeManager.setAttribute('speed', str(veh_dict[veh][index]['speed']))
nodeManager.setAttribute('type', str(veh_dict[veh][index]['type']))
nodeManager.setAttribute('x', str(veh_dict[veh][index]['x']))
nodeManager.setAttribute('y', str(veh_dict[veh][index]['y']))
root.appendChild(nodeManager)
topElem.appendChild(root)
fp = open('Manager.xml', 'w')
doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
推荐阅读
- android - 即使在启用 Places API 后,android 中的位置自动建议也不起作用
- ocr - 如何为 Tesseract 4.1.0 创建训练数据文件
- go - 同时收听 Go 通道数组
- java - Google Oauth By url For youtube api
- css - 边框半径在 Windows Outlook 上的电子邮件模板中不起作用
- session - Session中存储大量数据
- r - 在一定的权重条件下向数据集中添加一列
- javascript - 循环遍历计算属性 Vue
- android - 第一次没有得到服务器 android volley 的响应
- excel - 当 A 列(帐户代码)相同时,求和 B 列(借方)[Excel]