如何在xml文件中使用python插入值

时间:2018-12-03 11:56:03

标签: python xml

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

     Dim tagCollection As GeckoElementCollection = GeckoWebBrowser1.Document.GetElementsByTagName("div")
            For Each currettag As GeckoElement In GeckoWebBrowser1.Document.GetElementsByTagName("div")
                If currettag.GetAttribute("class") = "classname1" Then
                    currettag.???? // i can't convert ((GeckoHtmlElement)currentTag).Click() to vb.net code
                End If
            Next

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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