从XML文档获取所有文本? [英] Get all text from an XML document?
本文介绍了从XML文档获取所有文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以单个字符串的形式获取XML文档的所有文本内容-就像这个Ruby/hpricot示例,但使用Python.
How can I get all the text content of an XML document, as a single string - like this Ruby/hpricot example but using Python.
我想用一个空格替换XML标签.
I'd like to replace XML tags with a single whitespace.
推荐答案
这是我认为单空格缩进是正常的,并且在评论中提到它不是好答案.查看其他解决方案.仅出于存档原因,将其留在此处,请不关注它!
This is an answer posted when I thought one-space indentation is normal, and as the comments mention it's not a good answer. Check out the others for some better solutions. This is left here solely for archival reasons, do not follow it!
您要求输入lxml:
reslist = list(root.iter())
result = ' '.join([element.text for element in reslist])
或者:
result = ''
for element in root.iter():
result += element.text + ' '
result = result[:-1] # Remove trailing space
这篇关于从XML文档获取所有文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文