开发者

InnerText in Python

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜