-1

I am programming novice and have just started learning python below is my xml file:

    <Build_details>
      <Release number="1902">
        <Build number="260">
          <OMS>
            <Build_path>ST_OMS_V1810_B340</Build_path>
            <Pc_version>8041.30.01</Pc_version>
          </OMS>
          <OMNI>
            <Build_path>ST_OMNI_V1810_B340</Build_path>
          </OMNI>
        </Build>
      </Release>
      <Release number="1810">
        <Build number="230">
          <OMS>
            <Build_path>ST_OMS_909908</Build_path>
            <Pc_version>8031.25.65</Pc_version>
          </OMS>
          <OMNI>
            <Build_path>ST_OMNI_798798789789</Build_path>
          </OMNI>
        </Build>
      </Release>
      <Release number="1806">
        <Build number="300">
          <OMS>
            <Build_path>ST_OMS_V18102_B300</Build_path>
            <Pc_version>8041.30.01</Pc_version>
          </OMS>
          <OMNI>
            <Build_path>ST_OMNI_V18102_B300</Build_path>
          </OMNI>
        </Build>
      </Release>
    </Build_details>

How can i insert below chunk of data by asking release no to user and insert below it :
    <Build number="230">
      <OMS>
        <Build_path>ST_OMS_909908</Build_path>
        <Pc_version>8031.25.65</Pc_version>
      </OMS>
      <OMNI>
        <Build_path>ST_OMNI_798798789789</Build_path>
      </OMNI>
    </Build>

I need to search a particular release and then add details to it.Please help i am not unable to traverse xml to find a particular release

2 Answers2

1

I'm not able to add my comment because of less Reputations .

go through this link Reading XML file and fetching its attributes value in Python

Kabali
  • 382
  • 1
  • 3
  • 12
0

Here is the solution using python inbuilt library xml, You will have to find the release element first and then create a new build element and append to the release element.

import xml.etree.ElementTree as ET

if __name__ == "__main__":

    release_number = input("Enter the release number\n").strip()

    tree = ET.ElementTree(file="Build.xml") # Original XML File

    root = tree.getroot()

    for elem in root.iterfind('.//Release'):
        # Find the release element
        if elem.attrib['number'] == release_number:
            # Create new Build Element

            build_elem = ET.Element("Build", {"number": "123"})

            # OMS element
            oms_elem = ET.Element("OMS")
            build_path_elem = ET.Element("Build_path")
            build_path_elem.text = "ST_OMS_909908"
            pc_version_elem = ET.Element("Pc_version")
            pc_version_elem.text = "8031.25.65"
            oms_elem.append(build_path_elem)
            oms_elem.append(pc_version_elem)
            omni_elem = ET.Element("OMNI")
            build_path_omni_elem = ET.Element("Build_path")
            build_path_omni_elem.text = "ST_OMNI_798798789789"

            omni_elem.append(build_path_omni_elem)

            build_elem.append(oms_elem)
            build_elem.append(omni_elem)

            elem.append(build_elem)

            # Write to file
            tree.write("Build_new.xml") # After adding the new element
Optimus
  • 697
  • 2
  • 8
  • 22