开发者

InnerText in Python

开发者 https://www.devze.com 2023-04-11 18:20 出处:网络
Is there an equivalent to the c# XmlNode.InnerText Property in Python? If not, is there a straightforward way to implement it in Python? I thought I found something here, but it seems to concatenate

Is there an equivalent to the c# XmlNode.InnerText Property in Python?

If not, is there a straightforward way to implement it in Python? I thought I found something here, but it seems to concatenate the values of the node and child nodes in the wron开发者_运维技巧g order.


Here's a little recursive function that works with ElementTree:

def innertext(tag):
  return (tag.text or '') + ''.join(innertext(e) for e in tag) + (tag.tail or '')

Example usage:

import xml.etree.ElementTree as ET

root = ET.parse('somefile.xml').getroot()
print innertext(root)
0

精彩评论

暂无评论...
验证码 换一张
取 消