SoFunction
Updated on 2025-04-07

Python parses xml file and modifys its attribute value method

Python parses xml file and modifys its attribute value

This article mainly introduces the process of python parsing xml files, modifying their properties, and writing back. The blogger currently finds that there are two ways to implement them; however, the two implementations are as follows:

  • Using python's own package for xml parsing and writing back, it is impossible to add standalone='yes' attribute to xml file
  • Use the third-party package lxml for xml parsing and writing back. You can add the standalone='yes' attribute to the xml file.

For details, please see the following content

1. Use Python's own package for XML parsing

Implementation adds a property status= development to textCompent

  • The contents of the xml file are as follows
<?xml version='1.0' encoding='utf-8'?>
<config>
    <set_status>development</set_status>
    <component>
        <componentElement name="occN">
            <textCompent name="apk1" status="online" />
            <textCompent name="apk2" status="online" />
            <textCompent name="apk3" status="online" />
            <textCompent name="apk4" status="online" />
            <textCompent name="apk5" status="online" />
        </componentElement>
        <componentElement name="occK">
            <textCompent name="apk1" status="online" />
            <textCompent name="apk2" status="online" />
            <textCompent name="apk3" status="online" />
            <textCompent name="apk4" status="online" />
            <textCompent name="apk5" status="online" />
        </componentElement>
    </component>
</config>
  • Modified xml file
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<config>
    <set_status>development</set_status>
    <component>
        <componentElement name="occN">
            <textCompent name="apk1" status="development"/>
            <textCompent name="apk2" status="development"/>
            <textCompent name="apk3" status="development"/>
            <textCompent name="apk4" status="development"/>
            <textCompent name="apk5" status="development"/>
        </componentElement>
        <componentElement name="occK">
            <textCompent name="apk1" status="development"/>
            <textCompent name="apk2" status="development"/>
            <textCompent name="apk3" status="development"/>
            <textCompent name="apk4" status="development"/>
            <textCompent name="apk5" status="development"/>
        </componentElement>
    </component>
</config>
  • python script xml processing
import  as ET
config_file = ""


def change_xml():
    # parse the xml file into a tree    tree = (config_file)
    # Get the root node, <config> the following content    root = ()

    global status_set
    #Get embedded data, for example <set_status>development</set_status>    status_set = ("set_status").text

    component_list = ("component")
    for component in component_list.iter('componentElement'):
        for textCompent in ('textCompent'):
            # Get the current attribute value, for example <textCompens name="apk5" status="online" />            status=("status")
            # Set new attribute value            ("status",status_set)
    # xml_declaration means with file header, for example <?xml version='1.0' encoding='utf-8'?>    # When writing a file here, the standalone attribute cannot be set, otherwise an error will be reported.    ("", encoding="utf-8",xml_declaration=True)


if __name__ == '__main__':
    change_xml()

2. Use the third-party package lxml to perform xml operations

Implementation adds a property status= development to textCompent

  • Download the lxml package
pip install lxml
  • python script xml processing:

The only difference in writing methods found at present is: there are differences when parsing xml files and writing back xml files, and the others are the same

from lxml import etree
config_file = ""


def change_xml():
    # parse the xml file into a tree    tree = (config_file)
    # Get the root node, <config> the following content    root = ()

    global status_set
    #Get embedded data, for example <set_status>development</set_status>    status_set = ("set_status").text

    component_list = ("component")
    for component in component_list.iter('componentElement'):
        for textCompent in ('textCompent'):
            # Get the current attribute value, for example <textCompens name="apk5" status="online" />            status=("status")
            # Set new attribute value            ("status",status_set)

    # xml_declaration means with file header, for example <?xml version='1.0' encoding='utf-8'?>    # When writing files here, you can standalone attribute    ("", xml_declaration=True, pretty_print=True, encoding=,
                   standalone=)

if __name__ == '__main__':
    change_xml()
  • Modified xml file
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<config>
    <set_status>development</set_status>
    <component>
        <componentElement name="occN">
            <textCompent name="apk1" status="development"/>
            <textCompent name="apk2" status="development"/>
            <textCompent name="apk3" status="development"/>
            <textCompent name="apk4" status="development"/>
            <textCompent name="apk5" status="development"/>
        </componentElement>
        <componentElement name="occK">
            <textCompent name="apk1" status="development"/>
            <textCompent name="apk2" status="development"/>
            <textCompent name="apk3" status="development"/>
            <textCompent name="apk4" status="development"/>
            <textCompent name="apk5" status="development"/>
        </componentElement>
    </component>
</config>

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.