簡體   English   中英

如何在 python 中創建具有多個元素的 xml 文件

[英]How to create xml file with multiple element in python

我想使用 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")

我的 output 有所有數據,但它們都寫在這樣的“車輛”之一中:

<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 文檔的要求,在<vehicle>元素上方使用頂級根。 此外,避免重復行並使用內部字典鍵作為迭代器變量。 最后,使用上下文管理with ,將構建的 XML 寫入文件。

import xml.dom.minidom

# LIST OF DICTS
veh_dicts = [{'x': '469.2605', 'y': '5896.8761', 'time': 8.0, 'lane': '-250709918#7_0', 
              'angle': '11.1766', 'pos': '2.3000', 'speed': '0.0000', 'type': 'custom_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'}]

doc = xml.dom.minidom.Document()
root = doc.createElement('vehicles')             # TOP-LEVEL ROOT
doc.appendChild(root)

# ITERATE THROUGH EACH DICT
for i, veh in enumerate(veh_dicts, start=1):
   vehichleElem = doc.createElement('vehicle')
   vehichleElem.setAttribute('id', f'm{i}')      # USES F-STRING (Python 3.6+)
   root.appendChild(vehichleElem)
   
   nodeManager = doc.createElement('timestep')   
   for k in veh.keys():
      nodeManager.setAttribute(k, str(veh[k]))      
   vehichleElem.appendChild(nodeManager)
   
with open('MiniDomXMLBuild.xml', 'w') as fp:     # CONTEXT MANAGER (NO close() NEEDED)
    doc.writexml(fp, addindent='\t', newl='\n', encoding="utf-8")

Output

<?xml version="1.0" encoding="utf-8"?>
<vehicles>
    <vehicle id="m1">
        <timestep angle="11.1766" lane="-250709918#7_0" pos="2.3000" speed="0.0000" time="8.0" type="custom_moto" x="469.2605" y="5896.8761"/>
    </vehicle>
    <vehicle id="m2">
        <timestep angle="12.9664" lane="-250709918#7_0" pos="3.3001" speed="1.0001" time="9.0" type="custom_moto" x="470.1134" y="5907.0132"/>
    </vehicle>
</vehicles>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM