Python basics (12) Use ElementTree to parse XML

0. Preface

  • There are several ways to manipulate xml in Python, and I only care about the most common and simplest, that is, the ElementTree form.
  • Reference: Python official documentation
  • This article is only simple and practical, and does not involve passing aspects
    • Pull API for non-blocking parsing
    • Parsing XML with Namespaces
    • XPath support

1. Analysis operation

1.1. Read xml file

  • Read from file path
    • Assuming there is already a file path
    • You can pass tree = ET.parse('country_data.xml')
    • If you want to get the root node, you have to do one more step root = tree.getroot()
  • Read according to file content
    • It is assumed that the content of the file has been read as a string and saved.
    • You can root = ET.fromstring(xml_data_as_string)get the root object directly.

1.2. Get nodes and related information

  • Get the name of the node:Element.tag
  • Get nodes based on conditions
    • Element.iter('node_name')
      • Gets an iterator, fall all the name node_nameof the node.
      • All child nodes, child nodes of child nodes, etc. will be processed.
    • Element.findall(match): Get all matching nodes.
    • Element.find(match): Get the first matching node.

1.3. Get attributes and text

  • How to get node attributes
    • Element.attrib, Get a dictionary.
    • Element.get(key): Get the attribute value according to the key
  • Get text (for example <year>2020</year>):Element.text

2. Modify the file

2.1. Basic process

  • Construct ElementTree objects.
  • Modify the Element in the ElementTree object through some methods. The specific ones will be introduced a little later.
  • After modifying the ElementTree object, call the ElementTree.write(file_path)method to save the modified content to the local.

2.2. Modification of ElementTree

  • Modify attributes:Element.set(key, value)
    • As mentioned before, it Element.get(key)is to get attributes. This is the corresponding set method.
  • Add child elements:Element.append(son_element)
  • Delete child elements:Element.remove(son_element)
  • Create child elements for the specified element:ET.SubElement(father_element, 'son_element_tag')
  • Create elements out of thin air:ET.Element('tag_name')

3. Examples

  • The example comes from the official document , here is just a slight change.

  • Suppose there is a named country_data.xmlxml file, as follows

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

3.1. Analyze the instance

import xml.etree.ElementTree as ET

# 读取、解析文件,获取跟元素
tree = ET.parse('country_data.xml')
root = tree.getroot()

# 获取根元素的标签名称以及所有属性
# data
print(root.tag)
# country {'name': 'Liechtenstein'}
# country {'name': 'Singapore'}
# country {'name': 'Panama'}
print(root.attrib)

# 获取text
# 2008
print(root[0][1].text)

# 遍历名为 neighbor 的所有元素,并输出其对应的属性值
# {'name': 'Austria', 'direction': 'E'}
# {'name': 'Switzerland', 'direction': 'W'}
# {'name': 'Malaysia', 'direction': 'N'}
# {'name': 'Costa Rica', 'direction': 'W'}
# {'name': 'Colombia', 'direction': 'E'}
for neighbor in root.iter('neighbor'):
    print(neighbor.attrib)

# 一次性获取所有名为 country 的元素、然后遍历
# 寻找名为 rank 的子节点,获取其text
# 获取 country 元素中的 name 属性
# Liechtenstein 1
# Singapore 4
# Panama 68
for country in root.findall('country'):
    rank = country.find('rank').text
    name = country.get('name')
    print(name, rank)

3.2. Modification example

import xml.etree.ElementTree as ET

# 读取、解析文件,获取跟元素
tree = ET.parse('country_data.xml')
root = tree.getroot()

# 修改 root

# 修改rank元素的值,并增加属性updated,属性值为yes
for rank in root.iter('rank'):
    new_rank = int(rank.text) + 1
    rank.text = str(new_rank)
    rank.set('updated', 'yes')

# 删除所有rank大于50的country元素
for country in root.findall('country'):
    # using root.findall() to avoid removal during traversal
    rank = int(country.find('rank').text)
    if rank > 50:
        root.remove(country)

# 将修改内容写入本地文件中
tree.write('output.xml')

3.3. Build a new xml file

a = ET.Element('a')
b = ET.SubElement(a, 'b')
c = ET.SubElement(a, 'c')
d = ET.SubElement(c, 'd')

# 获取普通xml文件字符串
# <a><b /><c><d /></c></a>
print(ET.dump(a))

Guess you like

Origin blog.csdn.net/irving512/article/details/110682553