python elementtree module parse XML tree(二)

import elementtree.ElementTree as etree

mytree = etree.parse('test.xml')
myroot = mytree.getroot()

def func(ele,n):
#	global myroot
	for child in ele:
		child.set('deepth',str(n))
		if child.getchildren():
			func(child,n+1)	
				
func(myroot,1)
myxml = etree.tostring(myroot)
print(myxml)

如果我们有<tag1><tag2>...节点,如何把他们解析成tag1.tag2...的形式呢?

如果elementtree有取得标签深度的这个函数就方便的多了。可惜没有。实现起来就要修改原来的xml文件,即属性值添加一项。


有了深度之后,很容易就可以判断出树形结构了!

 


If you have any questions or ideas ,please feel free to contact me : )

thx.^^


QQ: 1623213673


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值