博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 解析 XML
阅读量:2167 次
发布时间:2019-05-01

本文共 3170 字,大约阅读时间需要 10 分钟。

xml文件:

taoge
13242008556
taoge1
13242008557
DB111111
DB222222
1
2008
141100
4
2011
59900
import xml.etree.ElementTree as etclass ParseXML:    """    解析 XML    """    def __init__(self, file, cust_no, apply_no):        # 初始化属性        print('parse xml...')        if file:            self.file = file        self.cust_no = cust_no        self.apply_no = apply_no    def get_root(self):        """        解析整个树对象,获取 tree、root        :return:        """        # 1. parse 方法解析 xml 文件,返回整个树对象 ElementTree        # tree = et.parse('example.xml')        tree = et.parse(self.file)        # 2. getroot 获取树对象的根结点对象 Element        root = tree.getroot()        '''                root.tag # 结点名称--str                root.attrib # 结点属性--dict                xxx.text # 最后一级元素的值        '''        return root    def get_child_khxx(self):        """        查找 khxx_info 节点中包含 cust_no 的子节点        :return:        """        root = self.get_root()        '''        # 3.1 遍历根结点,通过多级索引获取每个子结点        for child in root:            print(child.tag, child.attrib, child[0][0].tag, child[0][0].text)        '''        # 3.2 iter 方法直接遍历根结点中指定名称的子/孙结点        for khxx in root.find('KHXX_INFO'):            if self.cust_no == khxx.find('XM').text:                return et.tostring(khxx)            else:                return '''''    def get_child_xmxx(self):        """        查找 xmxx_info 节点中包含 apply_no 的子节点        :return:        """        root = self.get_root()        for xmxx in root.find('XMXX_INFO'):            xmbh = xmxx.find('XMBH').text            if xmbh.find(self.apply_no) != -1:                return et.tostring(xmxx)            else:                return '''''parsexml = ParseXML('example.xml', 'taoge1', 1)parsexml.get_child_khxx()# parsexml.get_child_xmxx()'''# 3.3 findall 方法获取当前节点的子结点中指定的所有元素# 不能查找孙结点# find 方法只获取当前节点指定的第一个子结点for child in root:    # print(child)    for khxx in child.findall('KHXX'):        print(khxx.tag)# 写 xml# 修改、添加元素for rank in root.iter('rank'):    new_rank = int(rank.text) + 1    rank.text = str(new_rank)  # 修改元素值    rank.set('updated', 'yes')  # 设置属性值tree.write('output.xml')  # write 把 xml 树对象写到文件中# remove 删除元素# 删除包含 rank > 3 元素的节点for country_info in root.findall('country_info'):    for country in country_info.findall('country'):       rank = int(country.find('rank').text)       if rank > 3:            country_info.remove(country)tree.write('output.xml')# tostring()函数将 root 对象转化为字符串xml_str = et.tostring(root)print(xml_str)# fromstring()函数使用字符串重新构造一个Element对象,并赋值给root变量root = et.fromstring(xml_str)print(root.tag)'''

转载地址:http://kypzb.baihongyu.com/

你可能感兴趣的文章
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
leetcode 130. Surrounded Regions
查看>>
【托业】【全真题库】TEST2-语法题
查看>>
博客文格式优化
查看>>
【托业】【新托业全真模拟】疑难语法题知识点总结(01~05)
查看>>
【SQL】group by 和order by 的区别。
查看>>
【Python】详解Python多线程Selenium跨浏览器测试
查看>>
Jmeter之参数化
查看>>
Shell 和Python的区别。
查看>>
Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
查看>>
Loadrunner之https协议录制回放报错如何解决?(九)
查看>>
python中xrange和range的异同
查看>>
列表、元组、集合、字典
查看>>
【Python】easygui小甲鱼
查看>>
【Python】关于Python多线程的一篇文章转载
查看>>
【Pyton】【小甲鱼】文件
查看>>
【Pyton】【小甲鱼】永久存储:腌制一缸美味的泡菜
查看>>
【Pyton】【小甲鱼】异常处理:你不可能总是对的
查看>>