Home  >  Article  >  Backend Development  >  In-depth understanding of XML operations in python

In-depth understanding of XML operations in python

零到壹度
零到壹度Original
2018-04-03 17:28:091387browse

This article mainly introduces the Xml operation of in-depth understanding of python. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor and take a look.

Read xml content:

 -*- coding:utf-8 -*-
# Author: Evan Mi
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
print(root.tag)
# 一个节点有tag、attrib、text三个值
# tag是标签的名字
# text是标签的内容
# attrib是标签属性的字典,通过字典的get('key')来获取对应的属性的值

# 直接for chile in parent 来遍历节点下的子节点
for child in root:
    print(child.tag, child.attrib)
    for elem in child:
        print(elem.tag, elem.text, elem.attrib)

# 只遍历year节点
for node in root.iter('year'):
    print(node.tag, node.text)

Generate xml content:

# -*- coding:utf-8 -*-
# Author: Evan Mi
import xml.etree.ElementTree as ET

new_xml = ET.Element('namelist')
name = ET.SubElement(new_xml, 'name', attrib={'enrolled': 'yes'})
age = ET.SubElement(name, 'age', attrib={'checked': 'no'})
sex = ET.SubElement(name, 'sex')
sex.text = '33'

name2 = ET.SubElement(new_xml, 'name', attrib={'enrolled': 'no'})
age = ET.SubElement(name2, 'age')
age.text = '19'

et = ET.ElementTree(new_xml)  # 生成文档对象
et.write('te.xml', encoding='utf-8', xml_declaration=True)

ET.dump(new_xml)  # 打印生成的格式

Modify and delete xml content:

# -*- coding:utf-8 -*-
# Author: Evan Mi
import xml.etree.ElementTree as ET

tree = ET.parse('test.xml')
root = tree.getroot()

# 修改
for node in root.iter('year'):
    new_year = int(node.text) + 1
    node.text = str(new_year)   # 修改内容
    node.set("updated", "yes")  # 修改属性

tree.write('tt.xml')


# 删除
for country in root.findall('country'):
    rank = int(country.find('rank').text)
    if rank > 50:
        root.remove(country)
tree.write('tt1.xml')

Related recommendations:

##Python & XML

python reading Write xml file

XML file operation in Python

The above is the detailed content of In-depth understanding of XML operations in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn