如何在python中创建包含多个元素的xml文件

如何在python中创建包含多个元素的xml文件,python,xml,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"

我想使用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>
  ........
<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" 
    .......
我的输出包含所有数据,但它们都写在一个“vehicle”中 像这样:

<?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>
  ........
<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")


根据需要,考虑在
元素上方使用顶级根。此外,避免重复的行,并使用内部字典键作为迭代器变量。最后,使用上下文管理器将构建的XML写入文件

import xml.dom.minidom
#口述清单
汽车驾驶执照=[{'x':'469.2605','y':'5896.8761','time':8.0','lane':'-250709918#7#0',
'角度':'11.1766','位置':'2.3000','速度':'0.0000','类型':'custom_moto'},
{'x':'470.1134','y':'5907.0132','time':9.0,'lane':'-250709918#7#0',
“角度”:“12.9664”,“位置”:“3.3001”,“速度”:“1.0001”,“类型”:“自定义”}]
doc=xml.dom.minidom.Document()
root=doc.createElement(“车辆”)#顶级根
追加子文档(根)
#反复阅读每一个单词
对于i,枚举中的veh(veh_dicts,start=1):
VehicleElem=doc.createElement(“车辆”)
setAttribute('id',f'm{i}')#使用f字符串(Python 3.6+)
root.appendChild(vehiclelem)
nodeManager=doc.createElement('timestep')
对于k in veh.keys():
setAttribute(k,str(veh[k]))
VehicleElem.appendChild(节点管理器)
使用open('MiniDomXMLBuild.xml','w')作为fp:#上下文管理器(不需要close())
doc.writexml(fp,addindent='\t',newl='\n',encoding=“utf-8”)
输出



您能提供您的车辆样本吗_dict@lrh09veh#u dict是一本字典。里面的例子是:
[{'x':'469.2605','y':'5896.8761','time':8.0,'lane':'-250709918#7#0','angle':'11.1766','pos'2.3000','speed':'0.0000','type':'custom u moto',{'x':'470.1134','y':'5907.0132','time':9.0,'lane':'-250709918#7#0','angle':'12.9664','pos':'3.3001','speed':'1.0001','type':'custom#moto']
您从哪里获取车辆
id
信息?它不在
车辆目录中。您所需的XML格式不正确。为了符合规则,您需要在
车辆
元素上方添加根标记。感谢您的回复。如果以这种方式执行,
XML.dom.hierarchyrequester:将出现两个不允许的文档元素
忘了。在XML中,你需要将所有元素嵌套在一个根元素中。所以你必须在顶部创建一个元素,比如<代码>车辆< /代码>,然后在它里面添加所有嵌套的<代码>车辆< /代码>元素。请检查更新的代码。如果有帮助的话,请考虑批准答案:“太好了。不要忘记堆栈溢出的方式。!