我编写了一个python代码,将包含内容的子元素插入到另一个更大的子元素中。它确实可以工作,但是问题是每个子元素由于某种原因两次插入。我检查了代码,但不确定为什么会发生,有人可以在代码中指出我的问题吗?我知道这不是最有效的方法,但我会尽快加以改进。
代码:
import random
import os
import xml.etree.ElementTree as ET
map_file = "D:\\Users\\micha\\Desktop\\ram-and-michael\\binaries\\data\\mods\\public\\maps\\scenarios\\TestML.xml"
units_file = os.listdir("D:\\Users\\micha\\Desktop\\ram-and-michael\\binaries\\data\\mods\\public\\simulation\\templates\\units") # all the units in the file.
armies_types = ("athen", "brit", "cart", "gaul", "iber", "kush", "mace", "maur", "pers", "ptol", "rome", "sele", "spart")
map_tree = ET.parse(map_file)
army_list = [] # units that can spawn in a game
army1 = generate_units() # returns a list of strings
army2 = generate_units()
flag = 10 # id
map_root = map_tree.getroot()
Entites = map_tree.find("Entities")
Entites = delete_Entitites(Entites)
for unit in army1:
Entity = ET.SubElement(Entites,"Entity", uid= str(flag))
Template = ET.SubElement(Entity,"Template").text = "units/" + unit
Player = ET.SubElement(Entity,"Player").text = "1"
Position = ET.SubElement(Entity,"Position",x="326.66935" ,z="321.1384")
Orientation = ET.SubElement(Entity,"Orientation",y="2.35621")
flag += 1
Entites.append(Entity)
print(flag)
for unit in army2:
Entity = ET.SubElement(Entites,"Entity", uid= str(flag))
Template = ET.SubElement(Entity,"Template").text = "units/" + unit
Player = ET.SubElement(Entity,"Player").text = "2"
Position = ET.SubElement(Entity,"Position",x="326.66935" ,z="321.1384")
Orientation = ET.SubElement(Entity,"Orientation",y="2.35621")
flag += 1
Entites.append(Entity)
print(flag)
indent(map_root)
map_tree.write(map_file,encoding = "utf-8",xml_declaration = True)
输出:
<Entities>
<Entity uid="10">
<Template>units/gaul_hero_vercingetorix_infantry</Template>
<Player>1</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="10">
<Template>units/gaul_hero_vercingetorix_infantry</Template>
<Player>1</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="11">
<Template>units/gaul_catafalque</Template>
<Player>1</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="11">
<Template>units/gaul_catafalque</Template>
<Player>1</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="12">
<Template>units/gaul_infantry_javelinist_e</Template>
<Player>1</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="12">
<Template>units/gaul_infantry_javelinist_e</Template>
<Player>1</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="13">
<Template>units/gaul_hero_vercingetorix</Template>
<Player>1</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="13">
<Template>units/gaul_hero_vercingetorix</Template>
<Player>1</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="14">
<Template>units/gaul_cavalry_swordsman_a</Template>
<Player>1</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="14">
<Template>units/gaul_cavalry_swordsman_a</Template>
<Player>1</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="15">
<Template>units/gaul_hero_vercingetorix_infantry</Template>
<Player>1</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="15">
<Template>units/gaul_hero_vercingetorix_infantry</Template>
<Player>1</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="16">
<Template>units/rome_infantry_swordsman_a</Template>
<Player>2</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="16">
<Template>units/rome_infantry_swordsman_a</Template>
<Player>2</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="17">
<Template>units/rome_catafalque</Template>
<Player>2</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="17">
<Template>units/rome_catafalque</Template>
<Player>2</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="18">
<Template>units/rome_champion_infantry_barracks</Template>
<Player>2</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
<Entity uid="18">
<Template>units/rome_champion_infantry_barracks</Template>
<Player>2</Player>
<Position x="326.66935" z="321.1384" />
<Orientation y="2.35621" />
</Entity>
</Entities>
谢谢您的帮助!
答案 0 :(得分:0)
创建子元素时,您指定了父元素,因此该子元素已添加到父元素中。
创建子元素后,您需要在以下行再次将其追加到父元素:
Entites.append(Entity)
因此您的SubElement将重复。