Python解析XML配置文件





1、ElementTree模块


ElementTree是Python处理XML文件的内置类,用于解析、查找和修改XML,ElementTree可以将整个XML文件解析成树形结构

单个Element的XML对应格式为:

<xxx attr="xxx">xxx</xxx>
 tag attrib     text
  • tag:XML标签,str对象
  • attrib:XML属性与值,dict对象
  • text:XML数据内容,str对象

例如下面test.xml这个XML文件:

<?xml version="1.0" encoding="utf-8"?>
<dev_info id="netmiko_inventory">
   <R1 type="cisco">
       <device_type>cisco_ios</device_type>
       <username>admin</username>
       <password>cisco</password>
       <ip>192.168.47.10</ip>
   </R1>
   <SW3 type="huawei">
       <device_type>huawei_vrpv8</device_type>
       <username>admin</username>
       <password>huawei</password>
       <ip>192.168.47.30</ip>
   </SW3>
</dev_info>

2、读取与解析


1) 标签、属性与值、内容获取汇总:

  • Element.tag:获取节点标签名,返回字符串类型
  • Element.attrib:获取节点属性与值,返回字典类型
  • Element.text:获取节点内容,返回字符串类型
from xml.etree import ElementTree as ET

# 读取XML文件
tree = ET.parse(r'C:\Users\cc\Desktop\test.xml')
# 获取根信息
root = tree.getroot()
print(root.tag)        # dev_info
print(root.attrib)     # {'id': 'netmiko_inventory'}
print(root.text)       # 空行
# 获取root的child层信息
for child in root:
    print(child.tag, child.attrib, child.text)
'''
R1 {'type': 'cisco'} 空行
SW3 {'type': 'huawei'} 空行
'''

2) ElementTree查找方法汇总:

  • Element.iter(tag=None):遍历Element的child,可以指定tag精确查找
  • Element.findall(match):查找当前元素tag或xpath能匹配的child节点
  • Element.find(match):查找当前元素tag或xpath能匹配的第一个child节点
  • Element.get(key,default=None):获取元素中key对应的value,如果没有key,返回default
for child in root.iter('password'):
    print(child.tag, child.attrib, child.text)
'''
password {} cisco
password {} huawei
'''
for child in root.findall('R1'):
    print(child.find('password').text)
'''
cisco
'''
# 获取元素属性对应的值,相当于attrib.get()
print(root.get('id'))  # netmiko_inventory

3、修改、写入、删除


3)修改、写入和删除方法汇总:

  • Element.text=new_text:修改数据内容
  • Element.remove(child):删除节点元素
  • Element.set(key,value):添加或修改属性attrib
  • Element.append(child):添加新的child节点

值得注意的是,修改完成后需要使用ElementTree.write()方法写入保存

# 修改R1的ip
for child in root.iter('R1'):
    # child.find('ip').text = str('192.168.47.1')
    child.set('ip', '192.168.47.1')

tree.write(r'C:\Users\cc\Desktop\test.xml')

# 删除SW3的标签
for child in root.findall('SW3'):
    root.remove(child)

tree.write(r'C:\Users\cc\Desktop\test.xml')

最终生成的XML内容如下:

'''
<dev_info id="netmiko_inventory">
   <R1 type="cisco" ip="192.168.47.1">
       <device_type>cisco_ios</device_type>
       <username>admin</username>
       <password>cisco</password>
       <ip>192.168.47.10</ip>
   </R1>
</dev_info>
'''

4、构建XML文档


ElementTree提供了两个静态函数(直接使用类名访问)Element和SubElement可以方便的构建XML

Element()用于创建新的元素,SubElement()为给定元素创建新的子元素

4)构建XML方法汇总:

  • Element(tag,attrib):创建元素,标签为tag,属性和值为attrib(字典类型或多个key=val)
  • SubElement(parent,tag,attrib):在parent元素下生成子元素,子元素标签为tag,属性和值为attrib(字典类型或多个key=val)
# Element(tag,attrib):创建元素,标签为tag,属性和值为attrib(字典类型或多个key=val)
root = ET.Element("tsRequest")
# SubElement(parent,tag,attrib):在parent元素下生成子元素,子元素标签为tag,属性和值为attrib(字典类型或多个key=val)
credentials_element = ET.SubElement(root, 'credentials', name='username', password='password')
ET.SubElement(credentials_element, 'site', contentUrl='site_url')
# dump():将元素树或元素结构写入sys.stdout,此函数仅用于调试
ET.dump(root)
'''
<tsRequest>
  <credentials name="username" password="password">
    <site contentUrl="site_url" />
  </credentials>
</tsRequest>
'''
# 将XML内容转换为二进制字符串
print(ET.tostring(root))
'''
b'<tsRequest><credentials name="username" password="password"><site contentUrl="site_url" /></credentials></tsRequest>'
'''

更多关于ElementTree的使用参考:https://www.bookstack.cn/read/python-3.12.0-zh/a5aab60c02b75e9c.md


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值