Bastien1533
Bastien1533

Reputation: 35

Create a child element under a particular parent Xml Python

I have an Xml file that I want to update, an extract from it:

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

I want to add a new texture entry only if the designator of the aircraft entry is A148 for example in order to have an output like this:

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

There is my code but I didn't manage to do what I wanted:

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)
        

The XML is contained in a file:

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

Thanks a lot for helping and I hope you have a great day!

Upvotes: 3

Views: 66

Answers (1)

Jack Fleeting
Jack Fleeting

Reputation: 24930

Assuming your data looks like this:

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

I would do the following:

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

Output:

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

Upvotes: 1

Related Questions