【问题标题】:How to insert values using python in xml file如何在xml文件中使用python插入值
【发布时间】:2018-12-03 11:56:03
【问题描述】:

我是编程新手,刚开始学习python 下面是我的xml文件:

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

我需要搜索特定版本,然后添加详细信息。请帮助 我无法遍历 xml 来查找特定版本

【问题讨论】:

标签: python xml


【解决方案1】:

由于声誉较低,我无法添加评论。

通过此链接Reading XML file and fetching its attributes value in Python

【讨论】:

    【解决方案2】:

    这里是使用python内置库xml的解决方案, 您必须先找到发布元素,然后创建一个新的构建元素并附加到发布元素。

    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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    相关资源
    最近更新 更多