Python XML file related operations

1. What is XML?

Extensible Markup Language XML is an extremely simple data storage language, and its structural features make it the most commonly used tool for data transmission between various applications. XML has no predefined tags, and users can customize XML tag storage and transmit any data they want to transmit and store according to their own needs.

  • Storage, which can be used to store configuration files, for example: java configuration files.
  • Transmission, exists in this format during network transmission, for example: data transmitted by early ajax, soap protocol, etc.
<?xml version="1.0" encoding="UTF-8" ?>
<!-- COTP request -->
<COTP>
    <Length>17</Length>
    <PDU_Type>0x0e</PDU_Type>
    <Destination_reference>0x0000</Destination_reference>
    <Source_reference>0x007</Source_reference>
    <Class>0</Class>
    <Extended_formats>False</Extended_formats>
    <No_explicit_flow_control>False</No_explicit_flow_control>
    <Parameter_code>0xc1</Parameter_code>
    <Parameter_length>2</Parameter_length>
    <Source_TSAP>0100</Source_TSAP>
    <Parameter_code>0xc2</Parameter_code>
    <Parameter_length>2</Parameter_length>
    <Destination_TSAP>0102</Destination_TSAP>
    <Parameter_code>0xc0</Parameter_code>
    <Parameter_length>1</Parameter_length>
    <TPDU_size>1024</TPDU_size>
</COTP>

2. XML-related operations

① Read the XML file and convert it to a string

This part only implements the operation of reading and converting the data in the level 1 and level 2 XML files into strings through iteration, and other more complex formats have not been converted yet.

import xml.etree.ElementTree as ET

def parse_xml_1(path):
    # ET打开xml文件
    tree = ET.parse(path)
    # 获取根标签
    root = tree.getroot()
    # print(root)

    message = ""
    # 获取root标签的孩子标签
    message = message + root.tag + '\r\n'
    i = 0
    for child in root:
        if i == 0:
            if child.text is not None:
                message += child.text
            else:
                pass
        else:
            if child.text is not None:
                message += ";" + child.text
            else:
                message += ";"
        i = i + 1

    return message

def parse_xml_2(path):
    # ET打开xml文件
    tree = ET.parse(path)
    # 获取根标签
    root = tree.getroot()
    # print(root)

    message = ""
    # 获取root标签的孩子标签
    message = message + root.tag + '\r\n'
    for child in root:
        message = message + child.tag + '\r\n'
        # print(child.tag, child.attrib, child.text)
        i = 0
        for node in child:
            # print(node.tag, node.attrib, node.text)
            if i == 0:
                if node.text is not None:
                    message += node.text
                else:
                    pass
            else:
                if node.text is not None:
                    message += ";" + node.text
                else:
                    message += ";"
            i = i + 1
        message = message + '\r\n'

    return message

The result of converting the previous XML file is as follows:

insert image description here
In addition, the specified node data can also be read through the find() and findall() functions. For more detailed operations in this part, see: Python Basics - XML ​​Format File for File Operation

# ET打开xml文件
tree = ET.parse(path)
# 获取根标签
root = tree.getroot()
# print(root)

PDU_Type_object = root.find("PDU_Type")
print(PDU_Type_object.tag, PDU_Type_object.attrib)

② Modify and delete nodes

import xml.etree.ElementTree as ET
 
# ET去打开xml文件
tree = ET.parse(path)
 
# 获取根标签 data
root = tree.getroot()
 
# 修改节点内容和属性
Length = root.find('Length')
print(Length.text)
Length.text = "999"
Length.set('update', '2020-11-11')
print(Length.text, Length.attrib)
# 保存文件
tree = ET.ElementTree(root)
tree.write("new.xml", encoding='utf-8')
 
# 删除节点
root.remove(root.find('Length') )
print(root.findall('Length'))
# 保存文件
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding='utf-8')

③ Build the XML file

The first type :

from xml.etree import ElementTree as ET
 
# 创建根标签
root = ET.Element("home")
 
# 创建节点大儿子
son1 = ET.Element('son', {
    
    'name': '儿1'})
# 创建小儿子
son2 = ET.Element('son', {
    
    "name": '儿2'})
 
# 在大儿子中创建两个孙子
grandson1 = ET.Element('grandson', {
    
    'name': '儿11'})
grandson2 = ET.Element('grandson', {
    
    'name': '儿12'})
son1.append(grandson1)
son1.append(grandson2)
 
# 把儿子添加到根节点中
root.append(son1)
root.append(son2)
 
tree = ET.ElementTree(root)
tree.write('oooo.xml', encoding='utf-8', short_empty_elements=False)

The generated XML is as follows:

<home>
    <son name="儿1">
        <grandson name="儿11"></grandson>
        <grandson name="儿12"></grandson>
    </son>
    <son name="儿2"></son>
</home>

The second type :

from xml.etree import ElementTree as ET
 
# 创建根节点
root = ET.Element("famliy")
 
 
# 创建大儿子
son1 = root.makeelement('son', {
    
    'name': '儿1'})
# 创建小儿子
son2 = root.makeelement('son', {
    
    "name": '儿2'})
 
# 在大儿子中创建两个孙子
grandson1 = son1.makeelement('grandson', {
    
    'name': '儿11'})
grandson2 = son1.makeelement('grandson', {
    
    'name': '儿12'})
 
son1.append(grandson1)
son1.append(grandson2)
 
 
# 把儿子添加到根节点中
root.append(son1)
root.append(son2)
 
tree = ET.ElementTree(root)
tree.write('oooo.xml',encoding='utf-8')

The generated XML is as follows:

<famliy>
    <son name="儿1">
        <grandson name="儿11"></grandson>
        <grandson name="儿12"></grandson>
    </son>
    <son name="儿2"></son>
</famliy>

The third type :

from xml.etree import ElementTree as ET
 
 
# 创建根节点
root = ET.Element("famliy")
 
 
# 创建节点大儿子
son1 = ET.SubElement(root, "son", attrib={
    
    'name': '儿1'})
# 创建小儿子
son2 = ET.SubElement(root, "son", attrib={
    
    "name": "儿2"})
 
# 在大儿子中创建一个孙子
grandson1 = ET.SubElement(son1, "age", attrib={
    
    'name': '儿11'})
grandson1.text = '孙子'
 
 
et = ET.ElementTree(root)  #生成文档对象
et.write("test.xml", encoding="utf-8")

The generated XML is as follows:

<famliy>
    <son name="儿1">
        <age name="儿11">孙子</age>
    </son>
    <son name="儿2"></son>
</famliy>

Guess you like

Origin blog.csdn.net/qq_43619058/article/details/125093808