欢迎访问宙启技术站
智能推送

Python中的XML解析器及其用法

发布时间:2023-12-11 16:56:10

Python中有很多可以用来解析XML的库,其中比较常用的有以下几个:

1. ElementTree

2. minidom

3. lxml

下面我将依次介绍这些库的用法,并给出相应的使用例子。

1. ElementTree:

ElementTree是Python标准库中内置的XML处理模块,它提供了一种解析XML的简单方法。使用ElementTree可以方便地处理XML文件的解析、创建以及修改。

示例如下:

import xml.etree.ElementTree as ET

# 解析XML文件
tree = ET.parse('example.xml')
root = tree.getroot()

# 遍历根节点
for child in root:
    print(child.tag, child.attrib)

# 获取指定节点
for neighbor in root.iter('neighbor'):
    print(neighbor.attrib)

# 修改节点属性
for country in root.findall('country'):
    rank = country.find('rank').text
    if int(rank) > 50:
        country.set('updated', 'yes')

# 创建新节点
new_country = ET.Element('country')
new_country.attrib['name'] = 'Argentina'
new_rank = ET.Element('rank')
new_rank.text = '75'
new_country.append(new_rank)
root.append(new_country)

# 保存修改后的XML文件
tree.write('new_example.xml')

2. minidom:

minidom是Python标准库中内置的另一个XML解析模块,它提供了一种更简单的解析XML的方法。使用minidom可以方便地处理XML文件的解析、创建以及修改。

示例如下:

import xml.dom.minidom as minidom

# 解析XML文件
doc = minidom.parse('example.xml')

# 获取根节点
root = doc.documentElement

# 遍历子节点
for node in root.childNodes:
    if node.nodeType == node.ELEMENT_NODE:
        print(node.nodeName)

# 获取指定节点
neighbors = doc.getElementsByTagName('neighbor')
for neighbor in neighbors:
    print(neighbor.nodeName)

# 修改节点属性
countries = doc.getElementsByTagName('country')
for country in countries:
    rank = country.getElementsByTagName('rank')[0]
    if int(rank.firstChild.data) > 50:
        country.setAttribute('updated', 'yes')

# 创建新节点
new_country = doc.createElement('country')
new_country.setAttribute('name', 'Argentina')
new_rank = doc.createElement('rank')
new_rank.appendChild(doc.createTextNode('75'))
new_country.appendChild(new_rank)
root.appendChild(new_country)

# 保存修改后的XML文件
with open('new_example.xml', 'w') as f:
    doc.writexml(f)

3. lxml:

lxml是Python中一个非常强大的第三方库,它是基于C语言的libxml2和libxslt库开发的,因此解析XML的效率非常高。使用lxml可以方便地处理XML文件的解析、创建以及修改。

示例如下:

from lxml import etree

# 解析XML文件
tree = etree.parse('example.xml')
root = tree.getroot()

# 遍历根节点
for child in root:
    print(child.tag, child.attrib)

# 获取指定节点
for neighbor in root.iter('neighbor'):
    print(etree.tostring(neighbor, encoding='utf-8').decode('utf-8'))

# 修改节点属性
for country in root.findall('country'):
    rank = country.find('rank').text
    if int(rank) > 50:
        country.set('updated', 'yes')

# 创建新节点
new_country = etree.Element('country', name='Argentina')
new_rank = etree.Element('rank')
new_rank.text = '75'
new_country.append(new_rank)
root.append(new_country)

# 保存修改后的XML文件
tree.write('new_example.xml', encoding='UTF-8', xml_declaration=True)

以上就是Python中常用的XML解析器及其用法的简单介绍,希望对你有所帮助!