简体   繁体   English

为什么我的Python代码将Subelement Twice插入到XML文件?

[英]Why does my Python code inserts a Subelement Twice to an XML file?

I wrote a python code that inserts subelements that contain content into another bigger subelement. 我编写了一个python代码,将包含内容的子元素插入到另一个更大的子元素中。 It does work but the problem is that every subelement is inserted twice for some reason. 它确实可以工作,但是问题是每个子元素由于某种原因两次插入。 I checked out the code and I am not sure why it happens, can anyone point me the problem in my code? 我检查了代码,但不确定为什么会发生,有人可以在代码中指出我的问题吗? I know it is not the most efficent way,but I'll try to improve it soon. 我知道这不是最有效的方法,但是我会尽快加以改进。
Code: 码:

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)

Output: 输出:

<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>

Thank you for any help! 感谢您的任何帮助!

When you create a SubElement you specify the parent, so it is already added to the parent element. 创建SubElement时,您需要指定父级,因此已经将其添加到父级元素中。

After creating your SubElement you are appending it to the parent again at line: 创建SubElement之后,您需要在以下行再次将其追加到父级:

Entites.append(Entity)

So your SubElement will repeat. 因此,您的SubElement将重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM