gpt4 book ai didi

python - 在特定的父 Xml Python 下创建子元素

转载 作者:行者123 更新时间:2023-12-04 03:35:01 27 4
gpt4 key购买 nike

我有一个要更新的 Xml 文件,其中的摘录:

<aircrafts>
<aircraft designator="A148" manufacturer="Antonov" name="Antonov An-148">
<textures>
<texture mtl_name="A148KOR" name="Air Koryo (KOR)"/>
<texture mtl_name="A148AGU" name="Angara Airlines (AGU)"/>
</textures>
</aircraft>
</aircrafts>

仅当飞机条目的指示符是 A148 时,我才想添加一个新的纹理条目,以便获得如下输出:

<aircrafts>
<aircraft designator="A148" manufacturer="Antonov" name="Antonov An-148">
<textures>
<texture mtl_name="A148KOR" name="Air Koryo (KOR)"/>
<texture mtl_name="A148AGU" name="Angara Airlines (AGU)"/>
<texture mtl_name="A148LNR" name="Lun'Air (LNR)"/> #new texture entry
</textures>
</aircraft>
</aircrafts>

这是我的代码,但我没有做到我想做的:

import xml.etree.ElementTree as ET

tree = ET.parse('X-Plane 11/Resources/plugins/ivao_pilot/PilotUI/data/mtlList.xml') #File where the extract is located
tree = tree.getroot()



print(tree[0].tag)
for i in range(100):
if tree[i].attrib['designator'] == "A148":
a = ET.SubElement(
tree[i],
"texture",
attrib={
"mtl_name": "A148LNR",
"name": "Lun'Air (LNR)"}

)
ET.dump(tree)

XML 包含在文件中:

<?xml version="1.0" encoding="UTF-8"?>
<aircrafts>
<aircraft designator="A10" manufacturer="Fairchild Republic" name="Fairchild Republic A-10 Thunderbolt II">
<textures>
<texture mtl_name="A10" name="US Air Force"/>
</textures>
</aircraft>
<aircraft designator="A124" manufacturer="Antonov" name="Antonov An-124 Ruslan">
<textures>
<texture mtl_name="A124" name="Antonov Design Bureau (ADB)"/>
<texture mtl_name="A124MXU" name="Maximus Air Cargo (MXU)"/>
<texture mtl_name="A124POT" name="Polet Air Company (POT)"/>
<texture mtl_name="A124RFF" name="Russian Federation Air Force (RFF)"/>
<texture mtl_name="A124VDA" name="Volga-dnepr (VDA)"/>
</textures>
</aircraft>
<aircraft designator="A148" manufacturer="Antonov" name="Antonov An-148">
<textures>
<texture mtl_name="A148KOR" name="Air Koryo (KOR)"/>
<texture mtl_name="A148AGU" name="Angara Airlines (AGU)"/>
<texture mtl_name="A148ADB" name="Antonov Design Bureau (ADB)"/>
<texture mtl_name="A148CUB" name="Cubana (An-158) (CUB)"/>
<texture mtl_name="A148POT" name="Polet Air Company (POT)"/>
<texture mtl_name="A148SDM" name="Rossiya (SDM)"/>
<texture mtl_name="A148RFF" name="Russian Federation Air Force (RFF)"/>
<texture mtl_name="A148SOV" name="Saratov Airlines (SOV)"/>
<texture mtl_name="A148UKN" name="Ukraine Air Enterprise (UKN)"/>
</textures>
</aircraft>
<aircraft designator="A225" manufacturer="Antonov" name="Antonov An-225 Mriya">
<textures>
<texture mtl_name="A225" name="Antonov Design Bureau (2010) (ADB)"/>
</textures>
</aircraft>
<aircrafts>

非常感谢您的帮助,希望您今天过得愉快!

最佳答案

假设您的数据如下所示:

aircrafts = """<aircrafts>
<aircraft designator="B100" manufacturer="Antonov" name="Antonov Bn-248">
<textures>
<texture mtl_name="B148KOR" name="Air Koryo (KOR)"/>
<texture mtl_name="B148AGU" name="Angara Airlines (AGU)"/>
</textures>
</aircraft>
<aircraft designator="A148" manufacturer="Antonov" name="Antonov An-148">
<textures>
<texture mtl_name="A148KOR" name="Air Koryo (KOR)"/>
<texture mtl_name="A148AGU" name="Angara Airlines (AGU)"/>
</textures>
</aircraft>
</aircrafts>
"""

我会做以下事情:

from lxml import etree
doc = etree.XML(aircrafts)
new_texture = etree.fromstring('<texture mtl_name="A148LNR" name="Lun\'Air (LNR)"/>')
for aircraft in doc.xpath('//aircraft[@designator="A148"]'):
destination = aircraft.xpath('./textures')[0]
destination.insert(2,new_texture)

print(etree.tostring(doc).decode())

输出:

<aircrafts>
<aircraft designator="B100" manufacturer="Antonov" name="Antonov Bn-248">
<textures>
<texture mtl_name="B148KOR" name="Air Koryo (KOR)"/>
<texture mtl_name="B148AGU" name="Angara Airlines (AGU)"/>
</textures>
</aircraft>
<aircraft designator="A148" manufacturer="Antonov" name="Antonov An-148">
<textures>
<texture mtl_name="A148KOR" name="Air Koryo (KOR)"/>
<texture mtl_name="A148AGU" name="Angara Airlines (AGU)"/>
<texture mtl_name="A148LNR" name="Lun'Air (LNR)"/></textures>
</aircraft>
</aircrafts>

关于python - 在特定的父 Xml Python 下创建子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67093754/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com